100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Conditionals in VBA

Learn how VBA evaluates Boolean expressions to branch code using If...Then...Else, ElseIf chains, and Select Case.

Control Flow & ProceduresBeginner8 min readJul 10, 2026
Analogies

Making Decisions with Conditionals

A conditional in VBA evaluates a Boolean expression—something that resolves to True or False—and runs a block of code only when that expression is True, letting a macro branch based on cell values, user input, or calculated results.

🏏

Cricket analogy: Just as a batter leaves or plays a delivery based on whether it pitches outside off stump, a conditional inspects a value and only swings into its code block when the condition reads True.

The If...Then...Else Structure

The core form is If condition Then ... Else ... End If. When the condition is True the Then block runs; otherwise the Else block runs. A single-line variant such as If x > 0 Then MsgBox "positive" omits End If, but the multi-line block form is preferred for readability and for running several statements per branch.

🏏

Cricket analogy: A captain sets either an attacking field or a defensive field depending on the run rate, two mutually exclusive plans just like the Then and Else blocks where exactly one runs.

vba
Sub GradeScore()
    Dim score As Integer
    score = Range("A1").Value

    If score >= 90 Then
        Range("B1").Value = "A"
    ElseIf score >= 80 Then
        Range("B1").Value = "B"
    ElseIf score >= 70 Then
        Range("B1").Value = "C"
    Else
        Range("B1").Value = "F"
    End If
End Sub

ElseIf for Multiple Branches

ElseIf chains let you test several conditions in order; VBA evaluates each until one is True, runs that block, and skips the rest. The optional final Else catches anything unmatched. Order matters—place the most specific or most likely conditions first, since evaluation stops at the first match.

🏏

Cricket analogy: A selector checks candidates in order—first-choice opener, then backup, then a promoted middle-order bat—stopping at the first available player, exactly how ElseIf stops at the first True test.

VBA uses And, Or, and Not for compound conditions, e.g. If age >= 18 And country = "US" Then. Unlike some languages, classic VBA does not short-circuit—both sides of And/Or are always evaluated—so avoid putting code with side effects or possible errors in the second operand.

Select Case for Cleaner Branching

When you are comparing one expression against many discrete values, Select Case is cleaner than a long ElseIf chain. It evaluates the test expression once and matches it against each Case, supporting lists (Case 1, 2, 3), ranges (Case 1 To 10), comparisons (Case Is > 100), and a Case Else fallback.

🏏

Cricket analogy: A scoreboard maps a dismissal code to its label—b, lbw, c&b, run out—switching on one value against many named outcomes, exactly what Select Case does.

vba
Sub CategorizeSale()
    Dim amount As Double
    amount = Range("A2").Value

    Select Case amount
        Case Is < 0
            MsgBox "Invalid amount"
        Case 0 To 99.99
            MsgBox "Small order"
        Case 100 To 999.99
            MsgBox "Standard order"
        Case Else
            MsgBox "Large order"
    End Select
End Sub

In VBA a single = serves as both assignment and comparison depending on context, so If x = 5 Then compares. A frequent bug is that an unset variable defaults to 0 or an empty string, silently making a condition take the wrong branch. Always initialize variables, and for objects test with Is Nothing rather than =.

  • If...Then...Else runs the Then block when the condition is True, otherwise the Else block; End If closes the multi-line form.
  • ElseIf chains test conditions top-down and stop at the first True match, so ordering matters.
  • Select Case matches one expression against many values, ranges (Case 1 To 10), or comparisons (Case Is > 100).
  • Compound conditions use And, Or, and Not, and VBA does not short-circuit them.
  • Case Else and the trailing Else act as catch-all branches for unmatched values.
  • Always initialize variables so conditions do not take a wrong branch on default zero or empty values.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#VBAStudyNotes#ConditionalsInVBA#Conditionals#VBA#Making#Decisions#StudyNotes#SkillVeris#ExamPrep