Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 184
Default Error on Set Rng statement?

Hi all. Any idea why this line errors out if the sheet Data is not
the active sheet? If I run the code while on the Data sheet, all
works well. If I run thwe code while on any other sheet, I get an
"application defined or object defined error". Thanks!

Set rng = Worksheets("Data").Range(Cells(1, 1), Cells(Rows.Count,
1).End(xlUp))
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Error on Set Rng statement?

Sub routine()
Set rng = Worksheets("Data").Range(Cells(1, 1), Cells(Rows.Count,
1).End(xlUp))
MsgBox (rng.Address)
End Sub

will work if:

1. there is a worksheet named Data
2. column A in that sheet has some data in it.
--
Gary''s Student - gsnu2007e


"Steve" wrote:

Hi all. Any idea why this line errors out if the sheet Data is not
the active sheet? If I run the code while on the Data sheet, all
works well. If I run thwe code while on any other sheet, I get an
"application defined or object defined error". Thanks!

Set rng = Worksheets("Data").Range(Cells(1, 1), Cells(Rows.Count,
1).End(xlUp))

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Error on Set Rng statement?

When you have an unqualified range in a general module, it will refer to the
activesheet.

When you have an unqualified range in a worksheet module, it will refer to the
sheet holding the code.

so this will work

Set rng = Worksheets("Data").Range(Worksheets("Data").Cells( 1, 1), _
Worksheets("Data").Cells(Rows.Count, 1).End(xlUp))

as will:

with Worksheets("Data")
Set rng = .Range(.Cells(1, 1), .Cells(Rows.Count, 1).End(xlUp))
End With

I'd use:

with Worksheets("Data")
Set rng = .Range("A1", .Cells(.Rows.Count, 1).End(xlUp))
End With


Steve wrote:

Hi all. Any idea why this line errors out if the sheet Data is not
the active sheet? If I run the code while on the Data sheet, all
works well. If I run thwe code while on any other sheet, I get an
"application defined or object defined error". Thanks!

Set rng = Worksheets("Data").Range(Cells(1, 1), Cells(Rows.Count,
1).End(xlUp))


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 184
Default Error on Set Rng statement?

Awesome. Thanks guys!!

On Mar 4, 4:28*pm, Dave Peterson wrote:
When you have an unqualified range in a general module, it will refer to the
activesheet.

When you have an unqualified range in a worksheet module, it will refer to the
sheet holding the code.

so this will work

Set rng = Worksheets("Data").Range(Worksheets("Data").Cells( 1, 1), _
* * * * * * * Worksheets("Data").Cells(Rows.Count, 1).End(xlUp))

as will:

with Worksheets("Data")
* Set rng = .Range(.Cells(1, 1), .Cells(Rows.Count, 1).End(xlUp))
End With

I'd use:

with Worksheets("Data")
* Set rng = .Range("A1", .Cells(.Rows.Count, 1).End(xlUp))
End With

Steve wrote:

Hi all. *Any idea why this line errors out if the sheet Data is not
the active sheet? *If I run the code while on the Data sheet, all
works well. *If I run thwe code while on any other sheet, I get an
"application defined or object defined error". *Thanks!


Set rng = Worksheets("Data").Range(Cells(1, 1), Cells(Rows.Count,
1).End(xlUp))


--

Dave Peterson


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
error 438 on For statement Janis Excel Programming 1 July 27th 07 04:16 PM
Runtime Error - Subscript out of range despite On Error statement DoctorG Excel Programming 3 July 28th 06 03:56 PM
For and IF statement error don Excel Programming 2 February 16th 06 07:03 PM
Path/File access error (Error 75) using Name Statement blayne Excel Programming 7 November 22nd 05 09:20 PM
Path/File access error (Error 75) after using Name Statement blayne Excel Programming 0 November 10th 05 12:33 AM


All times are GMT +1. The time now is 08:29 AM.

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

About Us

"It's about Microsoft Excel"