30 Advanced Excel Tips for 2026

Excel Champs / VBA / VBA Nested IF
By Puneet Gogia (Microsoft MVP)

VBA Nested IF

30+ Advanced Excel Tips for 2026

No spam. Only Excel Stuff. God Promise. 😊

Put one If block inside another and close each with its own End If. The inner test runs only when the outer one is True:

If Range("B2").Value = "East" Then
    If Range("C2").Value >= 10000 Then
        Range("D2").Value = "Bonus"
    Else
        Range("D2").Value = "No bonus"
    End If
End If

To test one value against several bands, use ElseIf instead of nesting. Each test runs only when the ones above it are False, so you need one End If and no upper limits:

If score >= 80 Then
    grade = "Excellent"
ElseIf score >= 60 Then
    grade = "Good"
ElseIf score >= 40 Then
    grade = "Average"
Else
    grade = "Poor"
End If

When both conditions must be met, If region = "East" And sales >= 10000 Then does the job of a nested pair on one line. VBA’s And always evaluates both sides, though, so keep the nested form when the second test would fail if the first is False, such as checking IsNumeric before comparing the value.

Indent each level so every End If lines up with its If; a missing one gives “Block If without End If”. An empty cell counts as 0 in a numeric test, so a blank score falls through to “Poor”. Check If IsEmpty(Range("A1")) Then first.

In VBA, you can use one IF statement inside another IF statement to create nested IFs. In simple words, you can execute one test statement using IF based on the result of another IF statement. This is useful when you need to test complex conditions. Let’s see an example:

vba-nested-if

In the above example, we are testing two conditions, condition1 (1 + 1 = 2) and condition2 (2 + 2 = 4).

But as we are using an IF statement inside another IF statement, VBA will first test the first condition, and only if it’s TRUE, move to the next IF.

In the second IF, we have a message box to show if the condition is TRUE.

Sub myMacro1()

'first if statement
If 1 + 1 = 2 Then

    'second if statement
    If 2 + 2 = 4 Then

        MsgBox True

    End If

Else

    MsgBox False

End If

End Sub

Here’s an example of using multiple IFs in one IF.

There is one difference between the example that we used earlier and the above example: in this code, we have used a nested condition for “Else”.

If the first condition is not true, VBA will move to the “Else” and test the condition there, and if that condition is not true, it moves to the next one.

In this way, we have a total of four conditions to test.

Sub myMacro2()

Dim myScore As Single: myScore = Range("A1").Value

If myScore >= 80 Then

MsgBox "Excellent"

Else

    If myScore >= 60 And myScore < 80 Then
        MsgBox "Good"
    Else

        If myScore >= 40 And myScore < 60 Then
        MsgBox "Average"
        Else

            If myScore < 40 Then
            MsgBox "Poor"

            End If
        End If
    End If
End If

End Sub

This nested IF statement will test the score from cell A1 and show a message box accordingly.

As VBA reaches an Else only when the condition before it is false, you don’t need the And part in the inner conditions. For example, when VBA tests myScore >= 60, it already knows the score is below 80.

Use ElseIf Instead of Nesting in Else

When you nest an IF inside the Else part again and again, like in the score example, you can write the same thing with ElseIf. VBA tests each condition from top to bottom and runs only the first one that’s true, and you need just one End If:

Sub myMacro4()
Dim myScore As Single: myScore = Range("A1").Value
If myScore >= 80 Then
MsgBox "Excellent"
ElseIf myScore >= 60 Then
MsgBox "Good"
ElseIf myScore >= 40 Then
MsgBox "Average"
Else
MsgBox "Poor"
End If
End Sub

This gives the same results as myMacro2 and is much easier to read. When you test one value against many ranges like this, Select Case is another clean option.

VBA Nested IF with Multiple Lines (IFs)

You can also use two separate IFs to execute for both results (True and False).

In the above example, as you can see, we first have a condition to test, and if that condition is TRUE, you have another condition to test, and if that second condition is TRUE, you’ll get a message box.

And if the main condition is false, you have another condition to test, and you’ll get a message box if that condition is true.

Sub myMacro3()

If 1 + 1 = 2 Then

    If 2 - 1 = 1 Then

        MsgBox "Condition1 is TRUE."

    End If

Else

    If 3 - 2 = 1 Then

        MsgBox "Condition2 is TRUE."

    End If

End If

End Sub

Nested IF vs. And

When you only need to run the code if both conditions are true (and you have nothing for the Else part), you can test them in one IF with the And operator:

If Range("A1").Value >= 40 And Range("B1").Value = "Yes" Then
MsgBox "Passed"
End If

But there’s one thing you need to know: VBA tests both sides of And, even when the first one is false. So when the second condition can only be tested if the first one is true, like checking a cell that a Find may not have found, use a nested IF instead:

Sub CheckFound()
Dim myCell As Range
Set myCell = Range("A1:A100").Find(What:="Total")
If Not myCell Is Nothing Then
If myCell.Offset(0, 1).Value > 0 Then
MsgBox "The total is more than zero."
End If
End If
End Sub

If you write both conditions in one line with And here, VBA still runs myCell.Offset when nothing is found, and you get an error.

Tips to Write Nested IFs

  • Indent each level of IF, so you can see which End If closes which IF.
  • Every block IF needs its own End If. If you miss one, you get the “Block If without End If” compile error.
  • Keep the nesting to a few levels. When it goes deeper, use ElseIf, Select Case or And to make it simpler.

Frequently Asked Questions

How Many IFs Can I Nest in VBA?

VBA lets you nest many levels of IF, much more than you’ll ever need. But after three or four levels, the code gets hard to read, so use ElseIf or Select Case instead.

What Is the Difference Between ElseIf and a Nested IF?

ElseIf tests another condition when the previous ones are false, all in one IF statement with one End If. A nested IF is a full IF statement inside another one, and it runs only when the outer condition is true (or in its Else part).

Why Do I Get the “Block If without End If” Error?

One of your block IF statements doesn’t have its End If. Indent your code by levels and count the IF and End If lines; each IF that has code on the lines below it needs its own End If.

Puneet Gogia

Written by

Puneet Gogia

Microsoft MVP — Excel · Founder, Excel Champs

Twelve years of teaching Excel, and a decade before that using it as a data analyst. Every tutorial here is written from a file I actually built.

3M+ readers helped
1,000+ tutorials written
Since 2015 teaching Excel