Specifying multiple test conditions with the If...Then...ElseIf statement

The block statement If...Then...ElseIf specifies conditional execution of one or another group of statements, depending on whether one or more expressions evaluates to TRUE or FALSE.

The syntax is:

If condition Then

statements

[ ElseIf condition Then

statements ]

[ ElseIf condition Then

statements ] ...

[ Else

statements ]

End If

The line breaks in actual statements must appear just as shown in the syntax diagram, and the contents of the If clause, the ElseIf clauses, and the Else clause must be written in the correct order.

Only one group of statements is executed: either the group following the first condition that evaluates to TRUE, or else those statements following the Else keyword. (If no condition evaluates to TRUE and there is no Else clause, then no statements are executed.) Once a group of statements is executed, no further condition expressions are evaluated; so the order of the ElseIf clauses is important. Program execution continues with the first statement following the End If keywords.

An If...Then...ElseIf statement not included within another statement can be skipped during execution only by executing a transfer of control: either by an Exit or End statement or by a transfer to a labeled statement, using GoTo, GoSub, and labels. All of these statements must be part of a procedure.

This example uses If..Then...ElseIf to determine whether a Timer value represents Morning, Afternoon, or Evening.

Dim timeTest As Single
timeTest! = Timer()   ' The Timer function returns
                      ' the number of seconds elapsed
                      ' since midnight.
If timeTest! < 43200 Then
   Print "Morning"
ElseIf timeTest! < 64800 Then
   Print "Afternoon"
Else
   Print "Evening"
End If

If you change the order of the contents of the If clause and the ElseIf clause, you can get a wrong result. For example, a Timer() value of 38017, represents a mid-morning time, but the example prints Afternoon.

Dim timeTest As Single
timeTest! = Timer()   ' The Timer function returns
                      ' the number of seconds elapsed
                      ' since midnight.
If timeTest! < 64800 Then
   Print "Afternoon"
ElseIf timeTest! < 43200 Then
   Print "Morning"
Else
   Print "Evening"
End If

This example uses If...Then...ElseIf statements to demonstrate changing a user-supplied whole number to an ordinal by adding the appropriate English suffix, such as "st" for 1 and "th" for 17. The script responds differently to numbers outside the range 0 to 50 (an arbitrary limit) and to numbers with a fractional part. There are three nesting levels of If...Then...ElseIf. Each statement needs its own End If phrase. An End If phrase ends the innermost statement running.

Dim anInt As String, lastDigit As String, printNum As String
anInt$ = InputBox$("Enter a whole number between 0 and 50:")
' Test for a number; print message if not, and do nothing more.
If Not IsNumeric(anInt$) Then
   MessageBox("That's not a number.")
' Test for whole number; print message if not, 
' and do nothing more.
ElseIf Fraction(CSng(anInt$)) <> 0 Then
   MessageBox("That's not a whole number.")
Else
   ' Test for number within required range.
   If CInt(anInt$) <= 50 And CInt(anInt$) >= 0 Then
      ' Number is within range. Find and append 
      ' the correct suffix.  
      lastDigit$ = Right$(anInt$, 1)
      If lastDigit$ = "1" And anInt$ <> "11" Then
         printNum$ = anInt$ & "st"
      ElseIf lastDigit$ = "2" And anInt$ <> "12" Then
         printNum$ = anInt$ & "nd"
      ElseIf lastDigit$ = "3" And anInt$ <> "13" Then
         printNum$ = anInt$ & "rd"
      Else
         printNum$ = anInt$ & "th"
      End If
       ' Print the ordinal in a message box.
      MessageBox("This is the " & printNum$ & " number.")
   Else
      ' Number is out of range. Print message, 
      ' and do nothing more.
      MessageBox("That number's out of range.")
   End If
End If
' Output:
' (For user input 3): "This is the 3rd number."
' (For user input -5.1): "That's not a whole number."
' (For user input 51): "That number's out of range."
' (For user input abacus): "That's not a number."

The example would be easier to read if the conditional processing were not nested three levels deep. If the main logic of this script were made into the contents of a procedure, it could be rewritten more simply.