VBA If Then Else Statements

Make decisions in your code with conditional logic and branching

Quick Reference

If Then: Execute code if condition is true

If Then Else: Choose between two code paths

ElseIf: Test multiple conditions sequentially

Select Case: Test one value against multiple cases

Basic If Then Statement

Execute code only when a condition is true.

Single Line Syntax:

If condition Then statement
Sub CheckValue()
If Range("A1").Value > 100 Then MsgBox "High value!"
End Sub

Multi-Line Syntax (Recommended):

If condition Then
' Code to execute
End If

Example 1: Simple Condition

Sub CheckScore()
Dim score As Integer
score = Range("B2").Value
If score >= 60 Then
MsgBox "Pass"
End If
End Sub

Example 2: String Comparison

Sub CheckStatus()
If Range("C1").Value = "Active" Then
Range("D1").Interior.Color = RGB(0, 255, 0)
End If
End Sub

If Then Else Statement

Execute one block of code if true, another if false.

Syntax:

If condition Then
' Code if true
Else
' Code if false
End If

Example 1: Pass/Fail

Sub GradeStudent()
Dim score As Integer
score = Range("B2").Value
If score >= 60 Then
Range("C2").Value = "Pass"
Range("C2").Font.Color = RGB(0, 128, 0)
Else
Range("C2").Value = "Fail"
Range("C2").Font.Color = RGB(255, 0, 0)
End If
End Sub

Example 2: Even/Odd Number

Sub CheckEvenOdd()
Dim num As Integer
num = InputBox("Enter a number:")
If num Mod 2 = 0 Then
MsgBox num & " is even"
Else
MsgBox num & " is odd"
End If
End Sub

ElseIf Statement

Test multiple conditions in sequence until one is true.

Syntax:

If condition1 Then
' Code if condition1 is true
ElseIf condition2 Then
' Code if condition2 is true
ElseIf condition3 Then
' Code if condition3 is true
Else
' Code if all conditions are false
End If

Example 1: Letter Grade

Sub AssignGrade()
Dim score As Integer
Dim grade As String
score = Range("A1").Value
If score >= 90 Then
grade = "A"
ElseIf score >= 80 Then
grade = "B"
ElseIf score >= 70 Then
grade = "C"
ElseIf score >= 60 Then
grade = "D"
Else
grade = "F"
End If
Range("B1").Value = grade
End Sub

Example 2: Age Category

Sub CategorizeAge()
Dim age As Integer
Dim category As String
age = Range("A2").Value
If age < 13 Then
category = "Child"
ElseIf age < 20 Then
category = "Teenager"
ElseIf age < 65 Then
category = "Adult"
Else
category = "Senior"
End If
Range("B2").Value = category
End Sub

Important: VBA evaluates conditions in order and stops at the first true condition. Order matters!

Comparison Operators

OperatorMeaningExample
=Equal toIf x = 5 Then
<>Not equal toIf x <> 0 Then
>Greater thanIf x > 10 Then
<Less thanIf x < 10 Then
>=Greater than or equalIf x >= 5 Then
<=Less than or equalIf x <= 5 Then

Logical Operators

Combine multiple conditions in a single If statement.

AND Operator

All conditions must be true.

If age >= 18 And age <= 65 Then
MsgBox "Working age"
End If

OR Operator

At least one condition must be true.

If day = "Saturday" Or day = "Sunday" Then
MsgBox "Weekend!"
End If

NOT Operator

Reverses the condition.

If Not IsEmpty(Range("A1")) Then
MsgBox "Cell has data"
End If

Combining Multiple Operators

If (score >= 90 And attendance >= 95) Or extraCredit = True Then
grade = "A+"
End If

Use parentheses to control evaluation order

Nested If Statements

Place If statements inside other If statements for complex logic.

Example: Grade with Bonus

Sub GradeWithBonus()
Dim score As Integer, grade As String
score = Range("A1").Value
If score >= 90 Then
If score = 100 Then
grade = "A+" ' Perfect score
Else
grade = "A"
End If
ElseIf score >= 80 Then
grade = "B"
Else
grade = "C or below"
End If
Range("B1").Value = grade
End Sub

Tip: Deep nesting can be hard to read. Consider using ElseIf or Select Case instead.

Select Case Statement

Cleaner alternative to multiple ElseIf statements when testing one variable against many values.

Syntax:

Select Case testVariable
Case value1
' Code for value1
Case value2
' Code for value2
Case Else
' Code for all other values
End Select

Example 1: Day of Week

Sub DayMessage()
Dim dayNum As Integer
dayNum = Weekday(Now)
Select Case dayNum
Case 1 ' Sunday
MsgBox "Relax, it's Sunday!"
Case 2 To 6 ' Monday to Friday
MsgBox "Weekday - time to work!"
Case 7 ' Saturday
MsgBox "It's Saturday!"
End Select
End Sub

Example 2: Grade Letter

Sub GetGradeInfo()
Dim grade As String
grade = Range("A1").Value
Select Case UCase(grade)
Case "A"
MsgBox "Excellent! 90-100%"
Case "B"
MsgBox "Good! 80-89%"
Case "C"
MsgBox "Average. 70-79%"
Case "D"
MsgBox "Below average. 60-69%"
Case "F"
MsgBox "Failing. Below 60%"
Case Else
MsgBox "Invalid grade"
End Select
End Sub

Example 3: Range of Values

Sub CategorizeScore()
Dim score As Integer
score = Range("A1").Value
Select Case score
Case Is >= 90
MsgBox "Grade: A"
Case 80 To 89
MsgBox "Grade: B"
Case 70 To 79
MsgBox "Grade: C"
Case 60 To 69
MsgBox "Grade: D"
Case Else
MsgBox "Grade: F"
End Select
End Sub

When to use Select Case: When testing one variable against multiple values. Much cleaner than many ElseIf statements!

IIf Function (Inline If)

Shorthand for simple If-Then-Else in a single line.

Syntax:

result = IIf(condition, valueIfTrue, valueIfFalse)

Example 1: Pass/Fail

result = IIf(score >= 60, "Pass", "Fail")

Example 2: Discount Calculation

discount = IIf(total > 100, 0.1, 0) ' 10% if over $100

Warning: IIf evaluates BOTH true and false expressions. Can cause issues with division by zero or function calls with side effects.

Best Practices

Use Select Case for multiple values: Cleaner than many ElseIf statements
Order ElseIf from most to least likely: Improves performance
Always include End If: Required for multi-line If statements
Use parentheses with complex conditions: Makes logic clear
Indent consistently: Makes nested conditions easier to read
Add comments for complex logic: Explain the business rule
Consider Option Compare Text: For case-insensitive string comparisons

Common Mistakes

❌ Using = for assignment in If

If x = 5 Then y = 10 ' Correct: = is comparison
' Not: If x == 5 (VBA uses single =)

❌ Forgetting End If

If x > 5 Then
MsgBox "High"
' Missing End If causes error!

❌ Wrong Order in ElseIf

If score > 0 Then ' This catches everything!
grade = "Pass"
ElseIf score >= 90 Then ' Never reached!
grade = "A"

Put most specific conditions first!

Related VBA Tutorials

Generate Conditional Code Automatically

Let AI write your If-Then-Else logic with proper syntax and best practices

✓ No credit card required ✓ 5 free generations