Understanding and Using Conditional Compilation

When you run a procedure for the first time, Visual Basic converts the VBA statements you used into the machine code understood by the computer. This process is called compiling. You can also perform the compilation of your entire VBA project before you run the procedure by choosing Debug | Compile (name of the current VBA project).

You can tell Visual Basic to include or ignore certain blocks of code when compiling and running by using conditional compilation. Your procedure may behave differently, depending on the condition you set. For example, conditional compilation is used to compile an application that will be run on different platforms (Windows or Macintosh, Win16 or Win32 bit). Conditional compilation is also useful in localizing an application for different languages. The program code excluded during the conditional compilation is omitted from the final file; thus, it has no effect on the size or performance of the program.

To enable conditional compilation, you should use special expressions called directives. First you need to declare a Boolean (True or False) constant by using the #Const directive. Next, you check this constant inside the #If...Then... #Else directive. The portion of code that you want to compile conditionally must be surrounded by these directives. Notice that the If and Else keywords are preceded by a number sign (#).

If a portion of code is to be run, the value of the conditional constant has to be set to True (-1). Otherwise, the value of this constant should be set to False (0).

Declare the conditional constant in the declaration section of the module. For example:

#Const User = True

This declares the conditional constant named User.

In the procedure that follows, data is displayed in the Polish language when the conditional constant named verPolish is True. The WhatDay procedure calls the DayOfWeek function, which returns the name of the week based on the supplied date. To compile the program in the English language, all you have to do is change the conditional constant to False and Visual Basic will jump to the block of instructions located after the #Else directive.

1. Insert a new module into the current VBA project, and rename it Conditional.

2. Enter the following procedure and functions:

' declare a conditional compiler constant #Const verPolish = True

Sub WhatDay()

Dim dayNr As Integer

#If verPolish = True Then dayNr = WeekDay(InputBox("Wpisz date, np. 01/01/2000")) MsgBox "To bedzie " & DayOfWeek(dayNr) & "." #Else

WeekdayName #End If End Sub

Function DayOfWeek(dayNr As Integer) As String

DayOfWeek = Choose(dayNr, "niedziela", "poniedzialek", "wtorek", _ "sroda", "czwartek", "piatek", "sobota") End Function

Function WeekdayName() As String

Select Case WeekDay(InputBox("Enter date, e.g. 01/01/2000"))

Case 1

WeekdayName =

"Sunday"

Case 2

WeekdayName =

"Monday"

Case 3

WeekdayName =

"Tuesday"

Case 4

WeekdayName =

"Wednesday

Case 5

WeekdayName = "Thursday" Case 6

WeekdayName = "Friday" Case 7

WeekdayName = "Saturday" End Select

MsgBox "It will be " & WeekdayName & "." End Function

3. Run the WhatDay procedure. Because the conditional compilation constant (verPolish) is set to True at the top of the module, Visual Basic runs the Polish version of the WhatDay procedure. It asks for the user's input in Polish and displays the result in Polish. To run the English version of the code, set the verPolish constant to False, and rerun the procedure.

Instead of declaring the conditional compiler constants at the top of a module, you can choose Tools | (Debugging) Properties (Figure 13-15). When you use the Properties window, use the following syntax in the Conditional Compilation Arguments text box to enable the English version of the WhatDay procedure:

verPolish = 0

If there are more conditional compilation constants, each of the constants must be separated by a colon.

Figure 13-15:

The conditional compilation constant can be declared either at the top of the module or in the Properties window but never in both places.

Figure 13-15:

The conditional compilation constant can be declared either at the top of the module or in the Properties window but never in both places.

4. Comment out the #Const verPolish directive at the top of the module and enter the conditional compilation constant in the Properties dialog box, as shown in Figure 13-15. Then run the WhatDay procedure to see how the Else branch of your program is now executed for English-speaking users.

0 0

Post a comment