View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Les Les is offline
external usenet poster
 
Posts: 240
Default Excel VBA Range Selection Error

Activate the worksheet before attempting to select the range.

xlsWorksheet.Activate
xlsWorksheet.Range("A1:A5").Select



--
Les Torchia-Wells


"scottydel" wrote:

Hello,

I'm using Excel and Access 2003. From within Access I open an Excel doc
with VBA, and am having trouble selecting ranges in the Excel doc. The
behavior is inconsistent in that sometimes my code works, and sometimes it
doesn't.

Code Snippet 1: Works every time

xlsWorksheet.Range("A1:A5").NumberFormat = "#,##0"

Code Snippet 2: Works sometimes, doesn't work sometimes

xlsWorksheet.Range("A1:A5").Select <--- this line fails
With xlsApp.Selection
.NumberFormat = "#,##0"
End With

The error message is: "Select method of Range class failed"

It seems accessing a range's properties directly works every time, but
selecting a range first then trying to access the selection's properties
fails during the "Select" method.

This error usually does not happen when I first open Access/Excel. But as I
start to develop, it starts happening after awhile.

The funny part is for some of the code I have recorded a macro to obtain the
code, and the maco always returns code similar to Snippet 2, the one that
fails.

Could it have to do with Excel process running in the background (I'm
setting break points and debugging and leave some processes running)?

Any ideas?

Your thoughts are appreciated!

-Scott