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

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I ran a few tests:
VBA Code:
Sub testwin64()
Dim kk As Integer
#If Win64 Then
  kk = 64
#Else
  kk = 32
#End If
MsgBox (kk) ' this prints 64
End Sub

Sub testwin64a()
Dim Win64 As Boolean
MsgBox (Win64)   ' this prints false
End Sub

Sub testwin64b()
MsgBox (Win64)  ' this gives error variable not defined
End Sub
So I think my suggestion is correct, the scope of the preprocessor constant is just within the compiler, certainly on my machine
 
Upvote 0
Solution
I ran a few tests:
VBA Code:
Sub testwin64()
Dim kk As Integer
#If Win64 Then
  kk = 64
#Else
  kk = 32
#End If
MsgBox (kk) ' this prints 64
End Sub

Sub testwin64a()
Dim Win64 As Boolean
MsgBox (Win64)   ' this prints false
End Sub

Sub testwin64b()
MsgBox (Win64)  ' this gives error variable not defined
End Sub
So I think my suggestion is correct, the scope of the preprocessor constant is just within the compiler, certainly on my machine
Unfortunate. I was hoping to be able to declare global constants without having them take up memory, but I guess not. Thanks for your help.
 
Upvote 0
If they don't take-up memory, where are they meant to be stored?
 
Upvote 0
If they don't take-up memory, where are they meant to be stored?
I imagine since it's a preprocessor command it would replace any instance of the variable name with the value in the code while compiling, so it's hardcoded in instead of accessing it in memory.
 
Upvote 0
You're probably thinking about the #define preprocessor directive in the C language (or maybe other language), which simply substitutes each instance of text with the specified value, after which the code gets compiled.
 
Upvote 0
You're probably thinking about the #define preprocessor directive in the C language (or maybe other language), which simply substitutes each instance of text with the specified value, after which the code gets compiled.
Yeah I thought VBA had an equivalent
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,774
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