Set a constant as global (VBA)

jeffreybrown

Well-known Member
Joined
Jul 28, 2004
Messages
5,152
If I want to create a constant as global in VBA this will work

Code:
Public Const MyPassword As String = "mrc"

Another constant I would like to make global fails with the following error

Code:
Compile error: Expected: indentifier

I'm using Conditional Compilation Arguments, but I can't seem to understand how to change the scope to global.

In the declarations area of any module that uses the conditional compilation if have

Code:
Const #DEBUG_ = False

but when I can it to

Code:
Public #Const DEBUG_ = False

I get the error above.

How can I change the scope to global? Any thoughts?
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
You can define it -

Tools > VBA Project properties > General tab

In the Conditional Compilation Arguments textbox type in:
Code:
DEBUG_ = 0

Then you can reference it in your code eg
Code:
    #If DEBUG_ Then
        MsgBox "in debug mode"
    #Else
        MsgBox "NOT in debug mode"
    #End If

Not entirely sure what advantage this gives you instead of your public constant though.
 
Upvote 0
Thanks Colin...I'll do some testing.

In the end I'm looking for a way to change the constant once and have it apply to any module with out having to put the constant at the top of every module.
 
Upvote 0
I think your Public constant defined in a standard code module will do that, Jeff.
 
Upvote 0
Hi Colin,

Using the Conditional Compilation Arguments textbox > Tools > VBA Project properties > General tab works great for setting the argument as global.

Thanks I'm going to go with this method. Setting the constant at the top of a module for the DEBUG_ as global does not work for me. I still get the error as a posted above.
 
Upvote 0
Hi Jeff,
Using the Conditional Compilation Arguments textbox > Tools > VBA Project properties > General tab works great
Glad it worked. :)
Setting the constant at the top of a module for the DEBUG_ as global does not work for me.
Crossed wires, I think. What I meant was using a "normal" public constant rather than a conditional compilation constant would give you exactly the same result? ie. You just declare it once and you can use it in every code module.
 
Upvote 0

Forum statistics

Threads
1,213,532
Messages
6,114,177
Members
448,554
Latest member
Gleisner2

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