If...Then...Else Statement

Description

Conditionally executes a group of statements, depending on the value of an expression.

Syntax

If condition Then statements [Else elsestatements ]

Or, you can use the following, more versatile syntax:

If condition Then
[statements][ElseIf condition-n Then
[elseifstatements]] . . .[Else
[elsestatements]]End If

The If...Then...Else statement syntax has these parts:

Part

Description

condition

One or more of the following two types of expressions:

A numeric or string expression that evaluates to True or False. If condition is Null, condition is treated as False.

An expression of the form TypeOf objectname Is objecttype. The objectname is any object reference and objecttype is any valid object type. The expression is True if objectname is of the object type specified by objecttype; otherwise it is False.

statements

One or more statements separated by colons; executed if condition is True.

condition-n

Same as condition.

elseifstatements

One or more statements executed if associated condition-n is True.

elsestatements

One or more statements executed if no previous condition or condition-n expression is True.


Remarks

You can use the single-line form (first syntax) for short, simple tests. However, the block form (second syntax) provides more structure and flexibility than the single-line form and is usually easier to read, maintain, and debug.

Note With the single-line syntax it is possible to have multiple statements executed as the result of an If...Then decision, but they must all be on the same line and separated by colons, as in the following statement:


If A > 10 Then A = A + 1 : B = B + A : C = C + B

When executing a block If (second syntax), condition is tested. If condition is True, the statements following Then are executed. If condition is False, each ElseIf condition (if any) is evaluated in turn. When a True condition is found, the statements immediately following the associated Then are executed. If none of the ElseIf conditions are True (or if there are no ElseIf clauses), the statements following Else are executed. After executing the statements following Then or Else, execution continues with the statement following End If.

The Else and ElseIf clauses are both optional. You can have as many ElseIf clauses as you want in a block If, but none can appear after an Else clause. Block If statements can be nested; that is, contained within one another.

What follows the Then keyword is examined to determine whether or not a statement is a block If. If anything other than a comment appears after Then on the same line, the statement is treated as a single-line If statement.

A block If statement must be the first statement on a line. The Else, ElseIf, and End If parts of the statement can have only a line number or line label preceding them. The block If must end with an End If statement.

Tip Select Case may be more useful when evaluating a single expression that has several possible actions. However, the TypeOf objectname Is objecttype clause can’t be used with the Select Case statement.

See Also

#If ... Then ... #Else Directive, Select Case Statement.

Example

This example shows both the block and single-line forms of the If...Then...Else statement. It also illustrates the use of If TypeOf...Then...Else.


Number = 53    ' Initialize variable.Number < 10 Then
    Digits = 1Number < 100 Then
' Condition evaluates to True so the next statement is executed.
    Digits = 2
    Digits = 3If
' Assign a value using the single-line form of syntax.Digits = 1 Then MyString = "One" Else MyString = "More than one" 

Use If TypeOf construct to determine whether the Control passed into a procedure is a text box.


Sub ControlProcessor(MyControl As Control)
    If TypeOf MyControl Is CommandButton Then
        Debug.Print "You passed in a " & TypeName(MyControl)
    ElseIf TypeOf MyControl Is CheckBox Then
        Debug.Print "You passed in a " & TypeName(MyControl)
    ElseIf TypeOf MyControl Is TextBox Then
        Debug.Print "You passed in a " & TypeName(MyControl)
    End IfSub