Questions And Answers

More Tutorials

VBA Conditional Compilation

Changing code behavior at compile time

The #Const directive is used to define a custom preprocessor constant. These can later be used by #If to control which blocks of code get compiled and executed.

#Const DEBUGMODE = 1
 Const filepath As String = "C:\Users\UserName\Path\To\File.txt"
 Const filepath As String = "\\server\share\path\to\file.txt"
#End If

This results in the value of filepath being set to "C:\Users\UserName\Path\To\File.txt". Removing the #Const line, or changing it to #Const DEBUGMODE = 0 would result in the filepath being set to "\\server\share\path\to\file.txt".

#Const Scope

The #Const directive is only effective for a single code file (module or class). It must be declared for each and every file you wish to use your custom constant in. Alternatively, you can declare a #Const globally for your project by going to Tools >> [Your Project Name] Project Properties. This will bring up the project properties dialog box where we’ll enter the constant declaration. In the “Conditional Compilation Arguments” box, type in [constName] = [value]. You can enter more than 1 constant by separating them with a colon, like [constName1] = [value1] : [constName2] = [value2].

Pre-defined Constants

Some compilation constants are already pre-defined. Which ones exist will depend on the bitness of the office version you're running VBA in. Note that Vba7 was introduced alongside Office 2010 to support 64 bit versions of Office.


In this page (written and validated by ) you learned about VBA Conditional Compilation . What's Next? If you are interested in completing VBA tutorial, your next topic will be learning about: VBA Object Oriented.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.