Setting a global constant as worksheet?

cbrf23

Board Regular
Joined
Jun 20, 2011
Messages
241
I'm trying to set a global constant as a worksheet object.

I'm not a fan of globals usually, but it seems dumb (and its a pain in debugging) for me to keep passing pointers to the same sheet over and over.

I cant figure out how to set the constant as a worksheet.

If you type the below code into VBA, you'll find that the stuff I colored red in red is red in VBA, meaning its not the proper syntax. The bottom one is acceptable to VBA, but I get a compile error at run time that says "constant expression required".

Can anyone point me in the right direction?
Code:
[COLOR=red]Public Const G_wsCert as Worksheet Sheets("CERTIFICATION (2)")[/COLOR]
[COLOR=red]Public Const G_wsCert as Worksheet = Sheets("CERTIFICATION (2)")[/COLOR]
[COLOR=red]Public Const G_wsCert as Worksheet set = Sheets("CERTIFICATION (2)")[/COLOR]
[COLOR=red]Public Const G_wsCert as Worksheet set G_wsCert = Sheets("CERTIFICATION (2)")[/COLOR]
[COLOR=red]Public Const G_wsCert as Sheets("CERTIFICATION (2)")[/COLOR]
Public Const G_wsCert = Sheets("CERTIFICATION (2)")
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
You can't declare an Object constante. It won't compile.

The best you can do is to define the const as a string which holds the sheet name as follows :

Code:
Public Const G_wsCert As String = "CERTIFICATION (2)"

Sub Test()
    Set sh = Sheets(G_wsCert)
End Sub

But then again,you are still referencing the sheet .
 
Upvote 0
Or you could declare G_wsCert as a worksheet variable and make it refer to the worksheet at the start of you routine. Then you can refer to the worksheet by the variable throughout your project as follows :

Code:
Public G_wsCert As Worksheet

Sub Test()

    Set G_wsCert = Sheets("CERTIFICATION (2)")
    
    MsgBox G_wsCert.Name
    
    'rest of your code

End Sub
 
Upvote 0
I forgot about this thread. Lol. Might as well share with you the workaround I came up with. The example below is for worksheets, but you could do the same thing for any type of object...

I created a class called claGlobalSheets and then set all the worksheet objects I needed as read only properties. This way they they cannot be changed...just like a constant.
Code:
'In the class module for claGlobalSheets
Option Explicit
Public Property Get SheetOne() As Worksheet
    Set SheetOne= Sheets("MySheet1")
End Property
Public Property Get SheetTwo() As Worksheet
    Set SheetTwo= Sheets("MySheet2")
End Property
Public Property Get SheetThree() As Worksheet
    Set SheetThree= Sheets("MySheet3")
End Property
I declared a global variable oSheets:
Code:
Public oSheets As claGlobalSheets
And from my main calling procedure, I create an instance of the object:
Code:
Set oSheets = New ClaGlobalSheets
Once instantiated the oSheets object can be accessed from any module:
Code:
'you could write this in any module:
Sub testName()
    MsgBox oSheets.Name
End Sub
 
Last edited:
Upvote 0
Hi.

I don't see that you are gaining much from this Class workaroud. You are still required to reference the sheet over and over throughout the code.


What's the difference between :

Code:
Sub testName()
oSheets.SheetName 
End Sub
and :

Code:
Sub testName()
Sheets("SheetName")
 End Sub
 
Upvote 0
Because, when the worksheet name changes, I only have to change it inside the osheets property, instead of having to hunt through multiple modules and replace it everywhere.

Also, less chance of a typing error causing problems.
 
Upvote 0
Have you looked at code names?
http://www.mrexcel.com/forum/showthread.php?t=253546&highlight=codename

BTW, as written, the value of claGlobalSheets.SheetOne will change when the user activates a different workbook.

I was not familiar with using the code name....that is a much more elegant solution! Thank you!

You could chagne it to thisworkbook.sheets(whatever) if you didnt want to change with the active workbook.... Also, the example I typed on the fly for getting a message box with the name wont work as written either. I noticed that but couldnt edit the post because it was past the time limit for editing.

should be
Code:
osheets.sheetone.name
 
Upvote 0

Forum statistics

Threads
1,224,508
Messages
6,179,189
Members
452,893
Latest member
denay

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