Select Case

You can use the Select Case statement instead of multiple ElseIf statements in an If...Then structure when you want to compare the same expression with several different values. A Select Case statement provides a decision-making capability similar to the If...Then...Else statement, but it makes code more efficient and readable.

For instance, to add several more job classifications to the example in the preceding section, you can add more ElseIf statements, or you can write the function using a Select Case statement, as in the following code.


Function Bonus(jobClass, salary, rating)
    Select Case jobClass
        Case 1
            Bonus = salary * 0.1 * rating / 10
        Case 2
            Bonus = salary * 0.09 * rating / 10
        Case 3
            Bonus = salary * 0.07 * rating / 10
        Case 4, 5    'The expression list can contain several values...
            Bonus = salary * 0.05 * rating / 5
        Case 6 To 8    '...or be a range of values
            Bonus = 150
        Case Is > 8    '...or be compared to other values
            Bonus = 100
        Case Else
            Bonus = 0
    End Select
End Function

Notice that the Select Case structure evaluates a single expression at the top of the structure. In contrast, the If...Then...ElseIf structure can evaluate a different expression for each ElseIf statement. You can replace an If...Then...ElseIf structure with a Select Case structure only if each ElseIf statement evaluates the same expression.