The Select Case statement is one of the most powerful and efficient decision-making structures in Excel VBA. It provides a cleaner alternative to complex If...Then...Else statements when you need to evaluate a single expression against multiple possible values.
While If...Then...Else statements work well for simple conditions, they can become:
The Select Case structure solves these problems by providing a clear, organized way to handle multiple conditions.
The structure of Select Case...End Select is as follows:
Select Case testExpression Case value1 statements Case value2 statements Case value3 statements Case Else statements End Select
Key features of Select Case:
This example demonstrates how to use Select Case to assign remarks based on letter grades. This is ideal when you have discrete values to compare against.
Private Sub CommandButton1_Click() Dim grade As String grade = InputBox("Enter the grade (A, A-, B, C, E or F)") Select Case grade Case "A" MsgBox "High Distinction" Case "A-" MsgBox "Distinction" Case "B" MsgBox "Credit" Case "C" MsgBox "Pass" Case Else MsgBox "Fail" End Select End Sub
This example shows how to use range expressions in Case statements to convert numerical marks to letter grades.
Private Sub CommandButton1_Click() Dim mark As Single Dim grade As String mark = InputBox("Enter the mark (0-100)") Select Case mark Case 0 To 29 grade = "F" Case 30 To 49 grade = "E" Case 50 To 59 grade = "D" Case 60 To 69 grade = "C" Case 70 To 79 grade = "B" Case 80 To 100 grade = "A" Case Else grade = "Error! Mark must be between 0 and 100" End Select MsgBox "Grade: " & grade End Sub
To
keyword to specify rangesThis example converts the previous code into a reusable function that can be called from any Excel worksheet cell.
Function grade(mark As Integer) As String Select Case mark Case 0 To 29 grade = "F" Case 30 To 49 grade = "E" Case 50 To 59 grade = "D" Case 60 To 69 grade = "C" Case 70 To 79 grade = "B" Case 80 To 100 grade = "A" Case Else grade = "Error!" End Select End Function
=grade(B2)
You can test for multiple values in a single Case statement using commas:
Select Case dayOfWeek Case 1, 7 MsgBox "Weekend" Case 2 To 6 MsgBox "Weekday" Case Else MsgBox "Invalid day" End Select
Use the Is
keyword with comparison operators:
Select Case age Case Is < 13 MsgBox "Child" Case 13 To 19 MsgBox "Teenager" Case Is >= 20 MsgBox "Adult" End Select
Use Like operator with wildcards for pattern matching:
Select Case fileName Case "*.xls*" MsgBox "Excel file" Case "*.doc*" MsgBox "Word document" Case "*.ppt*" MsgBox "PowerPoint file" End Select
Scenario | Best Choice | Reason |
---|---|---|
Single variable with multiple possible values | Select Case | More readable and efficient |
Complex conditions with multiple variables | If...Then...Else | Select Case only tests one expression |
Range-based conditions | Select Case | Simpler syntax with To keyword |
Boolean (True/False) conditions | If...Then...Else | More straightforward for simple tests |
While Select Case is generally efficient, keep these points in mind:
Select Case is useful in many real-world scenarios: