Thread: Set Range error
View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Die_Another_Day Die_Another_Day is offline
external usenet poster
 
Posts: 644
Default Set Range error

Because Cells, unless otherwise told, references cells on the
ActiveSheet. So you are tell it in effect:
Set fromRange = Worksheets("AAdata").Range(ActiveSheet.Cells(2, 4), _
ActiveSheet.Cells(2, 11))
What you really want to tell it is this:
Set fromRange =
Worksheets("AAdata").Range(Worksheets("AAdata").Ce lls(2, 4), _
Worksheets("AAdata").Cells(2, 11))
Which could be shortened to this:
With Sheets("AAdata")
Set fromRange = .Range(.Cells(2,4),.Cells(2,11))
End With

Clear as mud?

Charles Chickering
Randy Spleen wrote:
I'd appreciate some insight about setting ranges with Excel 2003 VBA.
In particular, why does THIS work

Dim fromRange as Range
...
Worksheets("AAdata").Activate
Set fromRange = Range(Cells(2, 4), Cells(2, 11))



And THIS give a Run-time "Application-defined or object-defined error",
#1004, error:

Dim fromRange as Range
...
Set fromRange = Worksheets("AAdata").Range(Cells(2, 4), Cells(2, 11))


TIA
R. Spleen