If...Then...Else Statement

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

Syntax

Long form
If condition [ Then ]
    [ statements ]
[ Else If elseifCondition [ Then ]
    [ elseifStatements ] ]
...
[ Else
    [ elseStatements ] ]
End If
Short form
If condition Then statement [ Else elseStatement ]

Parts

condition
Required — A Boolean expression
Then
Optional in the long form
Required in the short form
statement(s)
Optional in the long form — One or more statements, executed only when condition is true..
Required in the short form — One statement
elseifCondition
Required after Else If — A Boolean expression calculated only when the condition or elseifCondition before this is false.
elseifStatements
Optional — One or more statements, executed only when an elseifCondition is true.
elseStatement(s)
Optional — One or more statements, executed only after the condition and any and all elseifCondition expressions are false
End If
Completes the long form

Details

Long form

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

Short form

You can use the short form for short, easy tests. There are not many statements that can be used with this form. They include assignment, method call, Call, Call Event, End, 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