The Select Case structure in Excel allows you to compare a value against a list of values, and then take different actions depending on the result of the comparison. It is similar to the IF statement, but with one important difference: the Select Case statement can only test for equality, whereas the IF statement can test for a variety of conditions. This makes the Select Case statement ideal for testing whether a value falls within a certain range.
Here is the basic syntax of the Select Case statement:
Select Case testexpression Case expression1 [statements1] Case expression2 [statements2] Case Else [statements3] End Select
testexpression: This is the value that you want to compare against the list of values. It can be any valid expression.
expression1, expression2, ...: These are the values that you want to compare against testexpression. If testexpression is equal to one of these values, then the corresponding set of statements will be executed.
[statements1], [statements2], ...: These are the sets of statements that will be executed if < strong >testexpression strong > is equal to < strong >expression1 strong > , < strong > expression2 strong > , etc. Note that these statements must be enclosed within curly braces.
Sub Example1() Dim intNumber As Integer intNumber = 5 Select Case intNumber Case 1 MsgBox "Number is 1" Case 2 MsgBox "Number is 2" Case 3 To 10 MsgBox "Number is between 3 and 10" Case Else MsgBox "Number is not 1, 2 or between 3 and 10" End Select End Sub