VBA - How to access range from sheet without activating

Jer Maine

New Member
Joined
Jun 23, 2017
Messages
15
Hi, I am using the following code to count the number of rows in a range. For some reason, it only works when I have the source sheet activated. I need it to work no matter what sheet you are on and bypass having to activate a specific sheet. The variable sheetname is a string with the name of the worksheet where the data is coming from. Does anyone know what is causing this to not work?

When I'm on the right sheet, I get rowcount of 15 which is correct. When I'm on a different sheet, I get an error that says: "Run-time error '1004': Application-defined or object-defined error". I have researched this error but have not been able to find what I am doing wrong. It looks correct to me based on what I've found. Any help is greatly appreciated.

Code:
rowcount = Worksheets(sheetname).Range("A2", Range("A1").End(xlDown)).Cells.Count
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
You have to specify the sheet on BOTH Rante references..

rowcount = Worksheets(sheetname).Range("A2", Worksheets(sheetname).Range("A1").End(xlDown)).Cells.Count
 
Upvote 0
Well, that totally worked! Thank you. Although I don't totally understand why it worked. It looks to me like both range parameters would "understand" that they belong to the initial Worksheets(sheetname) object.
 
Upvote 0
Well, that totally worked! Thank you. Although I don't totally understand why it worked. It looks to me like both range parameters would "understand" that they belong to the initial Worksheets(sheetname) object.

They would if you had used a With statement. Otherwise, you have to qualify it every time. If I am correct, if you don't, then Excel assumes the active sheet is the target.

Code:
With Worksheets(sheetname)
 rowcount = .Range("A2", .Range("A1").End(xlDown)).Cells.Count
End With
 
Last edited:
Upvote 0
It looks to me like both range parameters would "understand" that they belong to the initial Worksheets(sheetname) object.
Well that's where they gotcha. Because they don't

Each range statement is a separate command on it's own.

So
Range("A1") - this refers to whichever sheet happens to be active, say it's "Sheet1"
Worksheets(sheetname).Range("A1") - this refers to the sheet named by the sheetname variable.
If they are not the same sheet, = Error.
 
Upvote 0
Okay, great to know. I appreciate both of your help. I'm new to this forum, but it's been a great source of guidance so far.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top