Excel VBA Switch Statement: Step-by-Step Guide
In this article
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
- 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.
- 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.
- Write the procedure shell. Type
Sub CheckGrade()and press Enter; the editor addsEnd Sub. Everything goes between those lines. - Set up the test value. Declare a variable and assign it, for example
Dim score As Integerthenscore = Range("A1").Value. Select Case needs one expression to evaluate; it can be a variable, a cell value, or a calculation. - Start the Select Case block. Type
Select Case scoreon its own line. This is the expression every Case line will be compared against. - Add your Case lines. Each case goes on its own line followed by the code to run. For example:
Case Is >= 90MsgBox "A"Case Is >= 80MsgBox "B"
UseCase 1, 2, 3to match several exact values,Case 10 To 20for a range, andCase Is > 100for a comparison. You can combine them:Case 1 To 5, 10, Is > 20. - Add a Case Else. Finish with
Case Elseand a fallback line such asMsgBox "No match". Without it, nothing happens when no case matches, which can look like a bug. - Close the block. Type
End Selectafter the last case. VBA will not compile without it. - 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. - 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 > 90is invalid; writeCase Is > 90. The wordIsis required for comparisons. - Overlapping cases. VBA runs the first matching case and skips the rest. If
Case Is >= 80comes beforeCase 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
CLngorCDblbefore the Select Case. - Case-sensitive text. By default, VBA text comparison is case-sensitive in some contexts. If case should not matter, compare with
LCaseorUCaseon both sides. - Putting Select Case outside a procedure. It must sit between
SubandEnd Sub(orFunctionandEnd Function).
Tips
- Use Select Case when you are testing one value against three or more options. For two options, a plain
Ifis 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 Elseto 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
Isbefore 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 scorebefore 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.