If…Then…Else…End If Statement

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 block form syntax:


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

Arguments:

  • Condition: A valid expression. A numeric or string expression that evaluates to True or False. If condition is Null, condition is treated as 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 the 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. So though the single-line form is supported but its recommended to used block form.

Note:

Single Line Syntax is supported but not recommended.

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:

Dim A,B,C
A=11
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 (if any) is evaluated in turn. When a True condition is found, the statements following the associated Then are executed. If none of the ElseIf statements are True (or 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 statements as you want in a block If, but none can appear after the Else clause. Block If statements can be nested; that is, contained within one another.

What follows the Then keyword is examined to determine whether 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 block If must end with an End If statement.

Example: Conditional Statements

If statement - execute only one statement when a condition is true

Dim i, strVal
i=10
If i=10 Then strVal = "Hello"

Execute more than one statement when if condition is true, you must put each statement on separate lines, and end the statement with the keyword "End If":

Dim i, strVal
i=10
If i=10 Then
    If i=10 Then strVal = "Hello"
    i = i+1
End If

If...Then...Else statement - select one of two sets of lines to execute

execute a statement if a condition is true and execute Else other statement if the condition is not true, you must add the "Else" keyword

Dim i 
Dim strVal
i=5
If i=10 Then
    If i=10 Then strVal = "Hello"
    i = i+1
Else
    i= i+2
    strVal = "world"
End If

If...Then...ElseIf …Else statement - select one of many sets of lines to execute

execute a statement if a condition is true and execute another statement if the first condition is not true and another condition is true. You must add ElseIf keyword for all additional conditions followed by Else at last:


Dim i
Dim strVal
i=14
If i=10 Then
    If i=10 Then strVal = "Hello"
    i = i+1
Elseif i=11 Then
    i= i+2
    strVal = "world"
Else
    i= i+3
    strVal = "world"
End If