Select Case Statement

Syntax

Select Case expr
[Case caseexpr[, ...] [ : instruction ]
    statements]...
[Case Else [ : instruction ]
    statements]
End Select

Group

Flow Control

Description

Selects the appropriate case by comparing the expr with each of the caseexprs. Selects the Case Else part if no caseexpr matches. (If the Case Else is omitted, skip the entire Select...End Select block.) Only the statements from the matched Case up to the next Case are executed.

Parameters

caseexpr Description
expr Executes if equal.
Is < expr Executes if less than.
Is <= expr Executes if less than or equal to.
Is > expr Executes if greater than.
Is >= expr Executes if greater than or equal to.
Is <> expr Executes if not equal to.
expr1 To expr2 Executes if greater than or equal to expr1 and less than or equal to expr2.

Example


Sub Main
    S = InputBox("Enter hello, goodbye, dinner or sleep:")
    Select Case UCases
    Case "HELLO", "HI"
        Debug.Print "come in"
    Case "GOODBYE", "BYE"
        Debug.Print "see you later"
    Case "DINNER"
        Debug.Print "Please come in."
        Debug.Print "Dinner will be ready soon."
    Case "SLEEP"
        Debug.Print "Sorry."
        Debug.Print "We are full for the night"
    Case Else
        Debug.Print "What?"
    End Select
End Sub

See also

If, Choose( ), If( ), IIf( )