Using Is with the Case Clause

Sometimes a decision is made based on the testexpression's condition, such as whether it is greater than, less than, equal to, or uses some other relational operator (see Table 5-1). The Is keyword lets you use a conditional expression in a Case clause. The syntax for the Select Case clause using the Is keyword is shown below:

Select Case testexpression Case Is conditionl statements if conditionl is True Case Is condition2

statements if condition2 is True Case Is conditionN

statements if conditionN is True End Select

For example, let's compare some numbers:

Select Case myNumber Case Is <10

MsgBox "The number is less than 10" Case 11

MsgBox "You entered eleven." Case Is >=100

MsgBox "The number is greater than or equal to 100." Case Else

MsgBox "The number is between 12 and 99."

End Select

Assuming that the variable myNumber holds 120, the third Case clause is true, and the only statement executed is the one between the Case Is >=100 and the Case Else clause.

0 0

Post a comment