Can't use Option Explicit and Preprocessor constants together?

Mortiray

New Member
Joined
Oct 27, 2022
Messages
37
Office Version
  1. 365
Platform
  1. Windows
Hello, All,

I'm trying to use Option Explicit alongside preprocessor constants, like in the dummy code below:

VBA Code:
Option Explicit

#Const TEST_CONST = 123

Sub TEST_MOD()
    Debug.Print TEST_CONST
End Sub

When I try to run TEST_MOD, I get a "Compile Error: Variable not defined." It does the same thing if I put Option Explicit above the #Const declaration. Do preprocessor constants not work with Option Explicit in this way?
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
An update - I tried running this code without Option Explicit, and it outputted "" instead of 123. Is there some sort of setting I have to enable to use preprocessor constants?
 
Upvote 0
I would think that the scope of the preprocessor constants is only for the compiler so that once the compile is done and the code runs the constant is deleted, the runtime constant ( with the same name) is thus undefined
 
Upvote 0
I would think that the scope of the preprocessor constants is only for the compiler so that once the compile is done and the code runs the constant is deleted, the runtime constant ( with the same name) is thus undefined
Darn. I hoped that preprocessor constants would essentialy go through your code and replace any instant of the constant name with the value at compile time. I thought it'd be cleaner than using Const or Public Const to declare constants - guess not.
 
Upvote 0
With the # I may add.
That is so strange. I wonder if there's a setting on my excel VBA that needs to be changed. What are the chances you have any VBA Add-ins or References that I don't that are changing this outcome?
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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