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

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
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,224,823
Messages
6,181,173
Members
453,021
Latest member
Justyna P

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