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.
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 SubElseIf 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.
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 SubIn 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
1. In an ElseIf chain where two conditions are True, what happens?
2. Which Select Case clause tests the inclusive range 1 through 10?
3. Does VBA short-circuit its And and Or operators?
4. Which keyword closes a multi-line If block?
5. Which is a valid comparison Case?
Was this page helpful?
You May Also Like
Loops in VBA
Master repetition in VBA with counter-driven For...Next, collection-driven For Each, and condition-driven Do loops.
Subs and Functions in VBA
Understand VBA's two procedure types—action-performing Subs and value-returning Functions—plus arguments, scope, and UDFs.
Error Handling with On Error
Intercept run-time errors gracefully in VBA using On Error statements, the Err object, and disciplined cleanup patterns.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics