Select Case

for condition check we use if and else statements, but when we have multiple condition to check, we can not use the if and else condition as it makes code more complicated. 


Syntax
Select Case condition
Case 
condition-n [ condition-n ]]
Case Else [ else 
condition]]
End Select


If condition matches any Case condition expression, the statements following that Case clause are executed that case block up to the next Case clause, or, for the last clause, up to End Select.

Example

Sub selectcaseexample()
    Dim Number

Number = 8 ' intial value.

Select Case Number ' Evaluate Number.

Case 1 To 5 ' Number between 1 and 5, inclusive.
        MsgBox "Between 1 and 5"  ' The following is the only Case clause that evaluates to True.

Case 6, 7, 8 ' Number between 6 and 8.
        MsgBox "Between 6 and 8"

Case 9 To 10 ' Number is 9 or 10.
        MsgBox "Greater than 8"

Case Else ' Other values.
        MsgBox "Not between 1 and 10"
End Select

End Sub

Post a Comment

0 Comments