Defining a range from an inactive sheet

nsontag

New Member
Joined
Jan 3, 2011
Messages
5
Hi all,

I have the following code snippet, which is genereating the always lovely "Application-defined or object-defined error" message.

Please help if you know what I am doing wrong!

Thx,

Nolan

Code:
Sub setRange()
 Dim srcSht As Worksheet, srcRng As range
 Set srcSht = Worksheets("WBS")
 Set srcRng = ActiveWorkbook.Worksheets("WBS").range(Cells(2, 2), Cells(21, 3))
End Sub

note that, in this particular context I do have a sheet named WBS, but it is *not* active. If I run this code when WBS is active, it works fine. I am trying to avoid having to activate the sheet in order to define a range within that sheet.

I am working with Excel 2010.

Any help is much appreciated!
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Code:
Set srcRng = ActiveWorkbook.Worksheets("WBS").Range(Worksheets("WBS").Cells(2, 2), Worksheets("WBS").Cells(21, 3))

Or,

Code:
 Set srcRng = ActiveWorkbook.srcSht.Range(srcSht.Cells(2, 2), srcSht.Cells(21, 3))
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,908
Members
452,949
Latest member
beartooth91

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