Check if Worksheet Object is Set (VBA)

Threazy

New Member
Joined
May 22, 2012
Messages
13
I'm sure the answer is simple, but I haven't found the right search terms to find it! So I'll just ask it here instead! :-)

I've declared several worksheet variables as global parameters to use across several VBA subroutines:

Public ThisWS, ThatWS, TheOtherWS as Worksheet

and then set these in a global initialization routine:

Set ThisWS = ActiveWorkbook.Worksheets("This Worksheet")

I've run into a couple places where ThisWS should be set, but is not and throws an error - and I'm not sure why. Is there a way to check if ThisWS is set in the VBA code? For example, would

If ThisWS = Nothing then

work? (I haven't tried that yet - my suspicion is that it would NOT work.)

Thanks - Bill
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
This should work,

Code:
If ThisWS Is Nothing Then

But you might avoid the problem all together by declaring your variables properly:

Code:
Public ThisWS As Worksheet
Public ThatWS As Worksheet
Public TheOtherWS As Worksheet
 
Upvote 0

Forum statistics

Threads
1,223,723
Messages
6,174,107
Members
452,544
Latest member
aush

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