Vba Select Case in Excel
The "Select Case" statement is another logical function of Excel used to perform conditional execution of code. VBA CASE SELECT function in Excel is very useful when having a lot of different choices and having to branch from them. The advantage of SELECT CASE over IF THEN is that it is much more easy to read and implement.
This "Select" part defines the variable to be checked and the "Case" part defines the conditions
Syntax
Select Case <expression>
Case <condition1>
do result1
Case <condition2>
do result2
End Select
The Select Case statement can check for any number of conditions
Example: The following example selects a string variable and perform conditional execution of code
- Sub SelectCase()
- Dim str As String
- str = "Hello"
- Select Case str
- Case "Hello"
- MsgBox "Condition is True"
- Case "World"
- MsgBox "Condition is False"
- End Select
- End Sub
The important thing is, the statement executes until a true condition is met and the remaining conditions are ignored.
The Result is as shown below