Decisions in VBA: If…Then, And/Or and Select Case

Decisions in VBA: If…Then, And/Or and Select Case

📎 This article includes 1 downloadable practice file ↓

⏱ 3 min readUpdated 27 September 2026

📘 Excel VBA Course · Lesson 4 of 15 — see all lessons

In this article
  1. If…Then…Else
  2. Combining conditions: And, Or, Not
  3. Select Case: cleaner than many ElseIfs
  4. Comparing text safely
  5. IIf for one-liners
  6. A practical example: mark overdue invoices
  7. Try it yourself: step by step

Macros become useful when they can decide: flag overdue invoices, skip blank rows, apply different discounts. VBA gives you two tools for this — If and Select Case.

If…Then…Else

If Range("C2").Value > 100000 Then
    Range("D2").Value = "Needs approval"
ElseIf Range("C2").Value > 0 Then
    Range("D2").Value = "OK"
Else
    Range("D2").Value = "Check amount"
End If

VBA checks the conditions top to bottom and runs the first one that is True, then jumps to End If.

Combining conditions: And, Or, Not

If status = "Open" And dueDate < Date Then flag = "Overdue"
If region = "North" Or region = "East" Then team = "A"
If Not IsEmpty(Range("A2")) Then …
⚠️ VBA evaluates both sides of And/Or — it does not “short-circuit”. If Not rng Is Nothing And rng.Value > 0 still crashes when rng is Nothing. Split it into two nested Ifs.

Select Case: cleaner than many ElseIfs

Select Case Range("B2").Value
    Case "Gold":            discount = 0.15
    Case "Silver":          discount = 0.1
    Case "Bronze", "Basic": discount = 0.05
    Case Else:              discount = 0
End Select

Select Case can also test ranges of numbers:

Select Case score
    Case Is >= 90: grade = "A"
    Case 75 To 89: grade = "B"
    Case 60 To 74: grade = "C"
    Case Else:     grade = "Fail"
End Select

Try both examples step by step in the VBA Playground (“If…Then” and “Select Case” tabs).

Comparing text safely

Text comparison is case-sensitive by default: "yes" = "Yes" is False. Normalise first:

If LCase(Trim(Range("A2").Value)) = "yes" Then …

IIf for one-liners

Range("E2").Value = IIf(Range("C2").Value >= 0, "Profit", "Loss")

IIf is neat, but like And/Or it evaluates both results, so do not put anything that could error inside it.

A practical example: mark overdue invoices

Sub FlagOverdue()
    Dim r As Long, lastRow As Long
    With Worksheets("Invoices")
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        For r = 2 To lastRow
            If .Cells(r, "E").Value = "Open" And .Cells(r, "D").Value < Date Then
                .Cells(r, "F").Value = Date - .Cells(r, "D").Value & " days late"
                .Rows(r).Interior.Color = RGB(253, 230, 220)
            End If
        Next r
    End With
End Sub

Try it yourself: step by step

  1. Run L04_FlagBigInvoices: every invoice of ₹50,000 or more is shaded.
  2. Change the threshold to 25000 and run again.
  3. Add an ElseIf that colours invoices below ₹5,000 light blue.
  4. Rewrite the logic with Select Case .Cells(r, "H").Value and Case Is >= 50000.
  5. Practise interactively in the Select Case playground.

📎 Practice files for this article

  • 📗
    Practice workbook with all course macros (.xlsm)Open it, click Enable Content in the yellow bar, go to the Macros sheet and press any button. 60 sales rows included to test on.
    ⬇ XLSM · 29 KB

Free to use for learning. Files with macros (.bas) are plain text — import them with Alt+F11 → File → Import File, and always test on a copy.