Select Case Statement


Executes one of the statement blocks in the case based on the test variable.

Select Case testvar
   Case var1
            Statement Block
   Case var2
            Statement Block
   Case Else
            Statement Block
End Select

The syntax supported by the Select statement includes the “To” keyword, a coma delimited list and a constant or variable.

Select Case Number   ' Evaluate Number.

Case 1 To 5   ' Number between 1 and 5, inclusive.

...

' The following is the only Case clause that evaluates to True.

Case 6, 7, 8   ' Number between 6 and 8.

...

Case 9 To 10   ' Number is 9 or 10.

...

Case Else   ' Other values.

...

End Select

Example:

    ' This rather tedious test shows nested select statements and if

    ' uncommented, the exit for statement.

 

    Sub Test()

        For x = 1 to 5

            print x

            Select Case x

            Case 2

                Print "Outer Case Two"

            Case 3

                Print "Outer Case Three"

    '            Exit For

                Select Case x

                Case 2

                    Print "Inner Case Two"

                Case 3

                    Print "Inner Case Three"

    '                Exit For

                Case Else   ' Must be something else.

                    Print "Inner Case Else:", x

                End Select

                Print "Done with Inner Select Case"

            Case Else   ' Must be something else.

                Print "Outer Case Else:",x

            End Select

        Next x

        Print "Done with For Loop"

    End Sub


Related Topic:

If...Then...Else Statement