Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Set Range error


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

  #2   Report Post  
Posted to microsoft.public.excel.programming
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


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Set Range error

because cells refers to the activesheet and range refers to AAdata.

Just change it to be expressed properly
with Worksheets("AAdata")
Set fromRange = .Range(.Cells(2, 4), .Cells(2, 11))
End with

then everything is pointing to the same sheet.

that said, since you are using fixed references

set fromRange = Wroksheets("AAdata").Range("D4:K4")

would work as well.

--
Regards,
Tom Ogilvy


"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


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Set Range error

If your code is in a general module, it should work.

But I'm guessing that your code is behind a worksheet. And it's not behind
AAdata.

Unqualifed ranges in a general module will refer to the activesheet.
Unqualified ranges behind a worksheet will belong to the worksheet that owns the
code.

I'd use:

with worksheets("aadata")
set fromrange=.range(.cells(2,4),.cells(2,11))
'or just
'set fromrange = .range("D2:K2")
end with



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


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 200
Default Set Range error

Die_Another_Day wrote:
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


Or shortened further to

Set rng = Sheets("AAdata").Range("A1")
Set fromRange = Range(rng(2, 4), rng(2, 11))

Alan Beban
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Runtime Error - Subscript out of range despite On Error statement DoctorG Excel Programming 3 July 28th 06 03:56 PM
Subscript out of range error - save copy error bg18461[_16_] Excel Programming 2 June 13th 06 04:53 PM
Subscript out of range error - save copy error bg18461[_15_] Excel Programming 1 June 13th 06 04:36 PM
Range.Select error and screen maximizing error in Workbook_Open() Punsterr Excel Programming 3 May 10th 06 10:16 PM
Range.Select error and screen maximizing error in Workbook_Open() Punsterr Excel Programming 0 May 10th 06 07:56 PM


All times are GMT +1. The time now is 07:49 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"