Select Case Statement
Executes one of several sets of statements based on the value of an expression.
Select Case testexpression
[Case Expressionlist-n
[Statements-n]] . . .
[Case Else Expressionlist-n
[Elsestatements-n]]
End Select
Parameters
Testexpression
An arbitrary numeric or string expression.
Expressionlist-n
An option is required if case appears. A list of the boundaries of one or more expressions.
Statements-n
One or more statements that are executed when the testexpression matches any part of the expressionlist-n.
Elsestatements-n
One or more statements that are executed when testexpression does not match any part of the case clause.
Description
If testexpression matches any case expressionlist expression, executes the statement between this case clause and the next one, and for the last clause, executes the statement between the clause and the end Select, and then the control is transferred to the E The statement after ND Select. If the testexpression matches the expressionlist expression in multiple case clauses, only the first matching statement is executed.
Case Else is used to indicate that if a match is not found between the testexpression and the expressionlist of any other option, the elsestatements is executed. Although not necessary, it is a good idea to put the case Else statement in a Select case block to handle unforeseen testexpression values. If no case expressionlist matches the testexpression and no case Else statement is executed, the statement after the end Select continues.
A Select Case statement can be nested, and each level of nested Select Case statements must have a matching End Select statement.
The following example illustrates how to use the Select Case statement:
Copy Code code as follows:
Dim Color, MyVar
Sub Changebackground (Color)
MyVar = LCase (Color)
Select Case MyVar
Case "Red" Document.bgcolor = "Red"
Case "green" Document.bgcolor = "green"
Case "Blue" Document.bgcolor = "Blue"
Case Else MsgBox "Choose another Color"
End Select
End Sub