Conditionally executes a sequence of statements as a result of a Boolean expression.
If condition [ Then ] [ statements ] [ Else If elseif_condition [ Then ] [ elseif_statements ] ] ... [ Else [ else_statements ] ] End [ If ]
If condition Then statement [ Else else_statement ]
- Necessary Boolean expression
- Optional in the many-line construction
- Necessary in the one-line construction
- Optional in the many-line construction – One or more statements, executed only when condition is true..
- Necessary in the one-line construction – One statement
- Necessary after Else If – A Boolean expression calculated only when the condition or elseif_condition before this is false.
- Optional – One or more statements, executed only when an elseif_condition is true.
- Optional – One or more statements, executed only after the condition and any and all elseif_condition expressions are false
- Completes the many-line construction. You can also use End If.
When an If...Then...Else statement becomes too long, you can replace it with a Select...Case Statement.
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.
If Then End If If Then Else End If If Then Else If Then Else If Then Else End If If Then Else