Basics of VBA / Macros Chapter – 17 (Select Case Part-1)

Basics of VBA / Macros Chapter – 17 (Select Case Part-1) 1
⏱ 4 min readUpdated 12 November 2026

Select Case

Instead of multiple If Then statements in Excel VBA, you can use the Select Case structure.

In this article
  1. Practical VBA workshop
  2. What you will build
  3. Complete VBA example
  4. How the solution works
  5. Run it step by step
  6. Common mistakes
  7. Make it production-ready
  8. Practice challenge
  9. Download the practice pack
  10. Continue learning

Situation:

Advertisement

Select Case in Excel VBA

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:

Select Case Result

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

  1. Download and unzip the practice pack below. Open the matching CSV or copy it into a new macro-enabled workbook.
  2. Press Alt + F11, choose File → Import File, and import this chapter’s .bas module.
  3. Rename the sample worksheet to the name used in the code, or change the worksheet name in one clearly marked line.
  4. Run the procedure with F5. Compare the result with the expected values and boundary cases in the CSV.
  5. 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.

⚡ Interactive Practice Lab & Formula Tester:

Test formulas dynamically in our web formula laboratory or download the companion Excel workbook.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free · AI can be wrong