Excecl VBA Classic Excel VBA 2010 Excel VBA 365 Excel VBA Examples About Us

Lesson 11: Mastering the Select Case Statement in Excel VBA


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.

11.1 Why Use Select Case?

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.

Basic Select Case Syntax

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:

Pro Tip: Select Case statements are generally faster than equivalent If...Then...Else structures because the expression is evaluated only once.

Example 11.1: Grade Evaluation

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
    

Key Points:

Example 11.2: Mark to Grade Conversion

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
    

Key Points:

Example 11.3: Creating a Custom Grade Function

This 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
    
Excel VBA Grade Function in Action
Figure 11.1: Using the grade function in an Excel worksheet

Key Points:

11.2 Advanced Select Case Techniques

Multiple Conditions in a Single Case

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
    

Comparison Operators

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
    

String Pattern Matching

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
    

11.3 When to Use Select Case vs. If...Then...Else

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

Best Practice: For 3 or more conditions testing the same variable, Select Case is almost always the better choice for readability and maintenance.

Common Mistakes to Avoid

  1. Forgetting End Select: Every Select Case must have a matching End Select.
  2. Overlapping Cases: VBA uses the first matching Case, so order matters when ranges overlap.
  3. Case Sensitivity: String comparisons are case-sensitive by default (use UCase or LCase if needed).
  4. Missing Case Else: Always include a Case Else to handle unexpected values gracefully.
  5. Complex Expressions: The test expression should be simple - move complex logic outside the Select Case.

Performance Considerations

While Select Case is generally efficient, keep these points in mind:

Practical Applications

Select Case is useful in many real-world scenarios:

Summary: Key Takeaways

  1. Select Case provides a cleaner alternative to complex If...Then...Else structures
  2. Ideal for evaluating a single expression against multiple possible values
  3. Supports exact matches, ranges, and comparison operators
  4. Always include a Case Else to handle unexpected values
  5. More readable and often more efficient than nested If statements
  6. Can be used in both procedures and worksheet functions
  7. Order of cases matters when ranges overlap
  8. String comparisons are case-sensitive by default

🔗 Related Resources



Copyright ® 2020 Dr.Liew Voon Kiong . All rights reserved   [Privacy Policy]

Contact: Facebook Page