Skip to main content

Understanding conditional compilation

Use conditional compilation to run blocks of code selectively, for example, debugging statements comparing the speed of different approaches to the same programming task, or localizing an application for different languages.

You declare a conditional compiler constant in code with the #Const directive, and you denote blocks of code to be conditionally compiled with the #If...Then...#Else directive.

The following example runs debug code or production code, based on the value of the variable.

' Declare public compilation constant in Declarations section.
#Const conDebug = 1
 
Sub SelectiveExecution()
    #If conDebug = 1 Then
    . ' Run code with debugging statements.
    .
    .
    #Else
    . ' Run normal code.
    .
    .
    #End If
End Sub

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>