If...Then...Else Statement

Conditionally executes a sequence of statements as a result of a Boolean expression.

Syntax

Many-line construction

If condition [ Then ]
    [ statements ]
[ Else If elseif_condition [ Then ]
    [ elseif_statements ] ]
...
[ Else
    [ else_statements ] ]
End [ If ]

One-line construction

If condition Then statement [ Else else_statement ]

Parts

condition
Necessary Boolean expression
Then
Optional in the many-line construction
Necessary in the one-line construction
statement(s)
Optional in the many-line construction – One or more statements, executed only when condition is true..
Necessary in the one-line construction – One statement
elseif_condition
Necessary after Else If – A Boolean expression calculated only when the condition or elseif_condition before this is false.
elseif_statements
Optional – One or more statements, executed only when an elseif_condition is true.
else_statement(s)
Optional – One or more statements, executed only after the condition and any and all elseif_condition expressions are false
End
Completes the many-line construction. You can also use End If.

Instructions

Many-line construction

When an If...Then...Else statement becomes too long, you can replace it with a Select...Case Statement.

One-line construction

You can use the one-line construction for short, easy tests. There are not many statements that can be used with this construction. They include assignment, method call, Call, Call Event, Exit, GoTo Case, ReDim, Return, and Throw.

Examples

If Then
End If

If Then
Else
End If

If Then
Else If Then
Else If Then
Else
End If

If Then Else

See also