IF ... Then ... Else and IF ... Then ... ElseIf

The If ... Then ... Else statement is used to define two blocks of conditions - true and false.

Example:
    

     If Age >=22 Then
        Drink = "Yes"
    Else
        Drink = "No"
    End If

Note that End If statement is needed in this case as well since there is more than one block of statements
 


The IF ... Then ... ElseIf is used to test additional conditions without using new If ... Then statements.

For Example:

   If Age >= 18 and Age < 22 Then
        Msgbox "You can vote"
    ElseIf Age >=22 and Age < 62 Then
        Msgbox "You can drink and vote"
    ElseIf Age >=62 Then
        Msgbox "You are eligible to apply for Social Security Benefit"
    Else
        Msgbox "You cannot drink or vote"
    End If

Note that the last condition under Else is, implicitly, Age < 18

Popular posts from this blog

50 Excel VBA Oral Interview Questions