Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA Switch Statement: Step-by-Step Guide

· 5 min read · Smart Home

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

A VBA Select Case statement is the Excel equivalent of a switch statement: it tests one expression against a list of cases and runs only the matching block. It is cleaner than a long chain of If...ElseIf lines and easier to read later. To use it, open the VBA editor with Alt+F11, insert a module, and write Select Case with your test value, then one Case line per value and an optional Case Else for anything unmatched.

What You'll Need

  • Excel on Windows or Mac with macros enabled. The VBA editor is not available in Excel for the web.
  • A workbook saved as .xlsm (macro-enabled) if you want to keep your code.
  • Basic familiarity with the VBA editor: Alt+F11 opens it, and Insert > Module adds a blank code module.

Step-by-Step

  1. Open the VBA editor. Press Alt+F11 (on Mac, Fn+Alt+F11 or the Developer tab > Visual Basic). If the Developer tab is hidden, right-click the ribbon and choose Customize the Ribbon to show it. Menu labels can differ slightly by Excel version and platform.
  2. Insert a module. In the editor, choose Insert > Module. A blank code window appears. Code in a standard module can be run from the Macros dialog or called from buttons.
  3. Write the procedure shell. Type Sub CheckGrade() and press Enter; the editor adds End Sub. Everything goes between those lines.
  4. Set up the test value. Declare a variable and assign it, for example Dim score As Integer then score = Range("A1").Value. Select Case needs one expression to evaluate; it can be a variable, a cell value, or a calculation.
  5. Start the Select Case block. Type Select Case score on its own line. This is the expression every Case line will be compared against.
  6. Add your Case lines. Each case goes on its own line followed by the code to run. For example:
    Case Is >= 90
    MsgBox "A"
    Case Is >= 80
    MsgBox "B"
    Use Case 1, 2, 3 to match several exact values, Case 10 To 20 for a range, and Case Is > 100 for a comparison. You can combine them: Case 1 To 5, 10, Is > 20.
  7. Add a Case Else. Finish with Case Else and a fallback line such as MsgBox "No match". Without it, nothing happens when no case matches, which can look like a bug.
  8. Close the block. Type End Select after the last case. VBA will not compile without it.
  9. Run the code. Press F5 inside the procedure, or go back to Excel and use Developer > Macros, select CheckGrade, and click Run. If the macro security setting blocks macros, Excel shows a yellow bar; enable content only for workbooks you trust.
  10. Test every branch. Change the value in A1 to something that matches each case, including a value that matches none, and confirm the right message appears each time.

Common Mistakes

  • Forgetting End Select. The editor flags a compile error, but the message points at the line after the block, not the missing line.
  • Using the wrong comparison syntax. Case score > 90 is invalid; write Case Is > 90. The word Is is required for comparisons.
  • Overlapping cases. VBA runs the first matching case and skips the rest. If Case Is >= 80 comes before Case Is >= 90, a score of 95 matches the 80 line. Order cases from most specific to least, or from highest to lowest.
  • Mixing text and numbers. A cell that looks like 90 may be stored as text. Compare like with like, or convert with CLng or CDbl before the Select Case.
  • Case-sensitive text. By default, VBA text comparison is case-sensitive in some contexts. If case should not matter, compare with LCase or UCase on both sides.
  • Putting Select Case outside a procedure. It must sit between Sub and End Sub (or Function and End Function).

Tips

  • Use Select Case when you are testing one value against three or more options. For two options, a plain If is shorter.
  • Keep each case block short. If a case needs many lines, move that work into its own Sub and call it.
  • Indent the code inside each Case so the structure is obvious at a glance.
  • Add a comment above the Select Case explaining what the expression represents; future-you will thank you.
  • Use Case Else to log or flag unexpected values rather than silently doing nothing.
  • For text matching, remember that Select Case supports Case "A" To "M" for alphabetical ranges.

Troubleshooting

  • Compile error: Expected: expression. Usually a Case line with nothing after it, or a missing Is before a comparison.
  • Compile error: Expected: End Select. You closed the Sub without End Select, or nested blocks are mismatched.
  • Nothing happens when you run it. Check that the test value actually matches a case. Add a temporary MsgBox score before the Select Case to see what is being tested.
  • Wrong branch runs. Look for overlapping cases and reorder them so the most specific match comes first.
  • Macro will not run. Check the macro security setting in the Trust Center and confirm the workbook is saved as .xlsm. If the file came from elsewhere, only enable macros if you trust the source.
  • Code runs on one machine but not another. Check for references to sheets or ranges that do not exist in the other workbook, and confirm both use the same macro settings.

Frequently Asked Questions

Does VBA have a Switch statement like other languages?

No. VBA uses Select Case, which works the same way: one expression tested against several cases, with an optional Case Else fallback.

Can Select Case test more than one value at once?

It tests one expression per Select Case block. To test two values, nest a second Select Case inside a case, or combine conditions with And inside an If.

What is the difference between Case 1, 2 and Case 1 To 2?

Case 1, 2 matches exactly 1 or exactly 2. Case 1 To 2 matches 1, 2, and any value in between, such as 1.5 for a decimal variable.

Can I use Select Case with text?

Yes. Case "Yes" matches that exact text. Use Case "A" To "M" for a range of starting letters, and convert case first if capitalization should not matter.

Why does my Case Else never run?

Because an earlier case is matching. Check for a broad condition such as Case Is > 0 placed above it, and reorder so the fallback is only reached when nothing else fits.

Is Select Case faster than If...ElseIf?

For a single value tested against many options, Select Case is usually clearer and can be marginally faster because VBA evaluates the expression once. The bigger benefit is readability.