Else If Clause and Nesting

If...Else...End If can be extended through the use of the ElseIF clause, and through nesting. Nesting is the technique of placing a block of code inside of another block of code of the same type. The following variation on our script illustrates both concepts:

Dim Greeting
Dim UserName
UserName = InputBox("Please enter your name:")
UserName = Trim(UserName)
If UserName = " " Then
Greeting = "Why won't you tell me your name? That's not very nice."
ElseIf UserName = "go away" Then
Greeting = "That's not very nice."
ElseIf UserName = "who's asking?" Then
Greeting = "I asked you first."
Greeting = "Hello, " & UserName & ", it's a pleasure to meet you."
If UserName = "Mike" Then
Greeting = Greeting & " I like the name Mike."
End If
End If
MsgBox Greeting

Once again, notice how the indenting identifies which lines of code are subordinate to the lines above them.