Excel VBA If Then Else Statement Syntax

VBA & macros · Updated November 22, 2026

The core syntax of the Excel VBA If Then Else statement is: If condition Then statements [Else elsestatements] for a single line, or the multi-line block form: If condition Then … Else … End If. The block form is the most common and readable, and it is what you should use for anything beyond a trivial one-liner.

Why the If Statement Matters in VBA

VBA (Visual Basic for Applications) is the programming language built into Excel. It lets you automate repetitive tasks, manipulate cells, and build custom functions. Almost every real-world macro needs to make decisions: “If this cell is empty, skip it,” “If sales exceed target, apply a bonus,” “If the user cancels, exit.” The If statement is the primary decision-making tool in VBA, just as it is in most programming languages. Understanding its syntax precisely is the difference between a macro that works and one that throws cryptic errors or silently does the wrong thing.

The Single-Line If Then Else

For a very short condition and a single action, you can write everything on one line. The syntax is:

If condition Then statement [Else elsestatement]

For example, if cell A1 contains a number and you want to set B1 to “High” if it’s greater than 100, otherwise “Low”:

If Range("A1").Value > 100 Then Range("B1").Value = "High" Else Range("B1").Value = "Low"

You can also omit the Else part: If Range("A1").Value = "" Then MsgBox "Cell is empty". The single-line form is compact, but it becomes hard to read when you have multiple statements or complex conditions. Also, note that in the single-line form, you do not use End If—the line itself is the complete statement.

The Block If Then Else (Multi-Line)

For anything more than a single action, use the block form. The full syntax is:

If condition Then
    ' statements to run if condition is True
ElseIf anothercondition Then
    ' statements to run if the first condition is False and anothercondition is True
Else
    ' statements to run if none of the above conditions are True
End If

The ElseIf and Else parts are optional. You can have as many ElseIf clauses as you need, but you can have at most one Else, and it must come last. Every block If must end with a line containing End If. Here is a practical example that checks the value in cell A1 and writes a message to B1:

Sub CheckValue()
    Dim cellValue As Variant
    cellValue = Range("A1").Value
    
    If cellValue > 100 Then
        Range("B1").Value = "High"
    ElseIf cellValue > 50 Then
        Range("B1").Value = "Medium"
    Else
        Range("B1").Value = "Low"
    End If
End Sub

Notice the indentation—it is not required, but it makes the code much easier to read. Also, the variable cellValue is declared with Dim, which is good practice. Always save a copy of your workbook before running any macro, because macros cannot be undone with Ctrl+Z.

Using ElseIf vs. Nested If Statements

When you have multiple conditions, you have two choices: chain them with ElseIf, or nest one If inside another. ElseIf is cleaner and easier to follow. For example, to categorize a score:

If score >= 90 Then
    grade = "A"
ElseIf score >= 80 Then
    grade = "B"
ElseIf score >= 70 Then
    grade = "C"
Else
    grade = "F"
End If

Nesting is useful when the inner condition only makes sense if the outer condition is true. For instance, checking if a cell is not empty before trying to convert it to a number:

If IsNumeric(Range("A1").Value) Then
    If Range("A1").Value > 0 Then
        MsgBox "Positive number"
    Else
        MsgBox "Zero or negative"
    End If
Else
    MsgBox "Not a number"
End If

Nesting too deeply (more than 3 or 4 levels) makes code hard to read. In that case, consider using Select Case, which is often a better alternative for many conditions (see the Alternatives section).

Common Pitfalls and How to Avoid Them

Several mistakes trip up beginners and even experienced developers:

  • Forgetting End If – In a block If, every If must have a matching End If. If you forget it, VBA will show a compile error: “Block If without End If”.
  • Using = instead of = for comparison – In VBA, = is used for both assignment and comparison. That is fine, but be careful: If x = 5 Then checks if x equals 5, while x = 5 assigns 5 to x. In an If condition, the = is always comparison.
  • Mixing single-line and block syntax – You cannot write If x > 0 Then MsgBox "Positive" Else MsgBox "Negative" End If on one line. If you use End If, you must use the block form with line breaks.
  • Comparing text with case sensitivity – By default, VBA string comparisons are case-insensitive. So If Range("A1").Value = "yes" Then will match “YES”, “Yes”, etc. If you need case-sensitive comparison, use StrComp with the vbBinaryCompare option.
  • Using And and Or incorrectly – In VBA, you use And and Or (not && or ||). Example: If x > 0 And x < 10 Then. Note that VBA does not short-circuit; both sides are always evaluated, so avoid calling functions with side effects in conditions.

Practical Examples with Cells and Loops

The most common use of If is to process a range of cells. For example, to loop through A1:A10 and highlight cells with negative values in red:

Sub HighlightNegatives()
    Dim cell As Range
    For Each cell In Range("A1:A10")
        If cell.Value < 0 Then
            cell.Interior.Color = vbRed
        End If
    Next cell
End Sub

Another example: check if a cell contains a formula (rather than a constant) using cell.HasFormula:

If cell.HasFormula Then
    MsgBox cell.Address & " contains a formula"
Else
    MsgBox cell.Address & " contains a value"
End If

You can also combine If with Exit Sub to stop a macro early if a condition is met. For instance, if the active sheet is not a worksheet, exit:

If TypeName(ActiveSheet) <> "Worksheet" Then
    MsgBox "Please select a worksheet."
    Exit Sub
End If

Alternatives: Select Case and IIf

When you have many conditions to test against a single expression, Select Case is often clearer than a long chain of ElseIf. The syntax is:

Select Case expression
    Case value1
        ' statements
    Case value2, value3
        ' statements
    Case Else
        ' statements
End Select

For example, to convert a numeric grade to a letter:

Select Case score
    Case 90 To 100
        grade = "A"
    Case 80 To 89
        grade = "B"
    Case 70 To 79
        grade = "C"
    Case Else
        grade = "F"
End Select

Another alternative is the IIf function, which is a one-liner that returns one of two values: IIf(condition, truepart, falsepart). For example: Range("B1").Value = IIf(Range("A1").Value > 100, "High", "Low"). However, IIf evaluates both the true and false parts, so if they have side effects (like function calls), it can be inefficient or cause errors. Use it only for simple value assignments.

Frequently Asked Questions

Can I use multiple statements after Then on one line?

Yes, in the single-line form you can separate multiple statements with colons. For example: If x > 0 Then y = 1 : z = 2. But this is rarely readable; the block form is better for multiple statements.

What is the difference between ElseIf and Else If?

In VBA, you must write ElseIf as one word. Writing Else If is a syntax error. The correct keyword is ElseIf.

How do I check if a cell is empty in VBA?

Use IsEmpty for a truly empty cell, or compare to an empty string: If Range("A1").Value = "" Then. Note that IsEmpty returns True only if the cell is completely empty; a formula that returns “” is not empty.

Can I use If inside a custom function (UDF)?

Yes, absolutely. You can use If statements inside a Function procedure just as you would in a Sub. For example, a custom function that returns “Pass” or “Fail” based on a score.

Why does my If statement not work when comparing text from a cell?

Often it is because the cell contains extra spaces or non-printing characters. Use Trim to remove leading/trailing spaces: If Trim(Range("A1").Value) = "Yes" Then. Also, remember that VBA is case-insensitive by default, so “yes” and “YES” are equal.

Is there a way to write an If statement without End If?

Yes, the single-line form does not require End If. But if you use the block form (with line breaks), you must include End If. There is no other way.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.