If Blocks

A basic component of a high-level language is the ability to construct a condition. Most high-level programming languages offer the If block as a way to evaluate an expression. Before proceeding into If blocks, I discuss what an expression is in terms of computer programming.

In programming terms, expressions are groupings of keywords, operators, and/or variables that produce a variable or object. Expressions are typically used to conduct calculations and manipulate or test data. Moreover, expressions can be used to build conditions, which return a Boolean value of True or False. This is an important concept, so I am repeating it in italics: Expressions can be used to build conditions that evaluate to True or False.

VBA programmers can use expressions in an If condition.

If (number1 = number2) Then

Me.Labell.Caption = "numberl equals number2" End If

Known as an If block, the preceding code reads "If the variable numberl equals the variable number2, then assign some text to the Caption property of Labell."This means the expression inside of the parentheses must evaluate to true for the VBA statement inside of the If block to execute. Note that the parentheses surrounding the expression is not required, but provide readability.

Also note the inclusion of the Then keyword at the end of the If statement. The Then keyword is required at the end of each If statement.

Always indent VBA statements inside of a condition or Loop to provide easy-to-read code. A common convention is to indent two or three spaces or to use a single tab. Doing so implies that the VBA assignment statement belongs inside the If block.

But what if the expression does not evaluate to true? To answer this question, VBA includes an Else clause, which catches the program's execution in the event the expression evaluates to false. The If/Else block is demonstrated next.

If (numberl = number2) Then

Me.Labell.Caption = "numberl Else

Me.Labell.Caption = "numberl End If

Giving the preceding examples, you might be asking yourself about other possibilities for building simple expressions with operators other than the equals sign. As shown in Table 3.1, VBA supports many common operators to aid in evaluating expressions.

Table 3.1 Common Operators Used in Expressions

Table 3.1 Common Operators Used in Expressions






Not equal


Greater than


Less than


Greater than or equal to


Less than or equal to

_j equals number2"

does not equal number2"

In addition to the Else clause, VBA provides the Elself clause as part of a larger expression. The Elself clause is one word in VBA and is used for building conditions that may have more than two possible outcomes.

If (numberl = number2) Then

Me.Labell.Caption = "numberl equals number2" Elself (numberl > number2) Then

Me.Labell.Caption = "numberl is greater than number2" Elself (numberl < number2) Then

Me.Labell.Caption = "numberl is less than number2" End If

Notice in the preceding example that the Elself clause must include an expression followed by the keyword Then, just like an If condition. In addition, you can use the Else clause to act as a concluding clause in the event that none of the conditions evaluates to true, as seen next.

Me.Labell.Caption = "The color is red" ElseIf (sColor = "white") Then

Me.Labell.Caption = "The color is white" ElseIf (sColor = "blue") Then

Me.Labell.Caption = "The color is blue" Else

Me.Labell.Caption = "The color is not red, white or blue" End If

0 0

Post a comment