
Select Case
Instead of multiple If Then statements in Excel VBA, you can use the Select Case structure.
In this article
Situation:

Place a command button on your worksheet and add the following code lines:
1. Declare two variables. One variable of type Integer named score and one variable of type String named result.
Dim score As Integer, result As String
2. Initialise the variable score with the value of cell A1.
score = Range(“A1”).Value
3. Add the Select Case structure.
Select Case score
Case Is >= 80
result = “very good”
Case Is >= 70
result = “good”
Case Is >= 60
result = “sufficient”
Case Else
result = “insufficient”
End Select
VBA uses the value of the variable score to test each subsequent Case statement to see if the code under the Case statement should be executed.
4. Write the value of the variable result to cell B1.
Range(“B1”).Value = result
5. Test the program.
Result when you click the command button on the sheet:

VBA executes the code under the second Case statement for all values greater than or equal to 70 and less than 80.
Practical VBA workshop
Goal: replace a long chain of repeated comparisons with a clear Select Case structure.
Order records contain status codes that must become readable labels and workflow actions.
What you will build
- A complete procedure that works without Select or Activate.
- Test data and expected behaviour you can verify before using real files.
- Error cases and production checks that prevent silent damage.
Complete VBA example
Option Explicit
Public Function StatusLabel(ByVal code As String) As String
Select Case UCase$(Trim$(code))
Case "N": StatusLabel = "New - validate order"
Case "A": StatusLabel = "Approved - release"
Case "H": StatusLabel = "On hold - investigate"
Case "C": StatusLabel = "Complete - archive"
Case "X", "VOID": StatusLabel = "Cancelled"
Case Else: StatusLabel = "Unknown code: " & code
End Select
End Function
How the solution works
- The Select Case expression is evaluated once, which keeps repeated comparisons short and consistent.
- Several exact values can share one Case line, as shown by X and VOID.
- Always include Case Else so unexpected source data is visible rather than silently treated as valid.
Run it step by step
- Download and unzip the practice pack below. Open the matching CSV or copy it into a new macro-enabled workbook.
- Press Alt + F11, choose File → Import File, and import this chapter’s
.basmodule. - Rename the sample worksheet to the name used in the code, or change the worksheet name in one clearly marked line.
- Run the procedure with F5. Compare the result with the expected values and boundary cases in the CSV.
- Undo is not available after a macro changes a workbook. Test on a copy, save only after checking totals, and keep the original source file.
Common mistakes
- Select Case is not automatically case-insensitive; normalize the input first.
- Overlapping ranges are legal, but only the first matching Case runs, so order matters.
- Do not hide unknown codes with an empty result; return or log enough context to diagnose the source.
Make it production-ready
Add Option Explicit, qualify every workbook and worksheet, validate the last row and input types, and use a single cleanup section to restore ScreenUpdating, EnableEvents and calculation settings after an error. Avoid hard-coded regulatory rates or colour-only business rules. Record the source, effective date and assumptions in a visible Notes sheet.
Practice challenge
Add codes R for Returned and P for Partially Shipped, then write a loop that fills labels in column B from codes in column A.
Test normal data, blanks, text in numeric columns, duplicate values and the exact threshold boundaries. A useful macro should fail clearly rather than produce a plausible but wrong answer.
Download the practice pack
Download VBA Chapters 13–23 practice pack (.zip)
The pack contains an importable VBA module and UTF-8 CSV test data for every chapter. Review code before enabling macros.
Test formulas dynamically in our web formula laboratory or download the companion Excel workbook.
Atul (AJ) builds office-automation and local-first AI tools as Atulya AI, and works with Excel, SAP and Oracle EPM every day. He writes about the shortcuts, macros and automations that actually save time — plus the occasional wacky fact. Videos on YouTube: Lazy Automator AI (@LazyAutomatorAI).
More posts by Atul →Stuck on a step? Ask a question and the AI answers using this article.