How to Solve “End If Without Block If” Error in VBA

Method 1 – Omit End If Statement in Single Line Statement

The End If without Block If error might occur if you write an If statement in a single line and insert an End If statement at the end like in the following code.

Sub SingleLine()
If Range("C5").Value > 40 Then Range("D5").Value = "Pass"
End If
End Sub

Single Line If Without End If VBA Code

  • Click Run and it will show the End If without block If error.

End If without Block If Error

Solution:

  • Remove the End If as it is not necessary when you write the If statement in one line.
Sub SingleLine()
If Range("C5").Value > 40 Then Range("D5").Value = "Pass"
End Sub

End If Removed

  • Run the code again and you get your desired output without any error.

End If without Block If Error Solved


Method 2 – Remove Extra End If Statements

The End If without Block If error can occur if you have more End If statements than If statements. In the following VBA, there are two If statements and three End If statements.

Sub ExtraEndIfs()
Dim score As Integer
For Each cell In Range("C5:C12")
score = cell.Value
        If score >= 90 Then
            cell.Offset(0, 1).Value = "A"
        ElseIf score >= 80 Then
            cell.Offset(0, 1).Value = "B"
        ElseIf score >= 70 Then
            cell.Offset(0, 1).Value = "C"
        ElseIf score >= 60 Then
            cell.Offset(0, 1).Value = "D"
        Else
            If score >= 50 Then
                cell.Offset(0, 1).Value = "E"
            Else
                cell.Offset(0, 1).Value = "F"
            End If
        End If
    End If
    Next cell
End Sub

Extra End If in VBA

  • You will get the same error if you run this code.

End If without Block If error

Solution:

  • Make sure that there is one End If statement for each If statement. Any extra End If must be removed before running the code.
  • There is one extra End If in the above code. Remove the “End If” to solve the error.
Sub ExtraEndIfs()
    Dim score As Integer
    For Each cell In Range("C5:C12")
        score = cell.Value
        If score >= 90 Then
            cell.Offset(0, 1).Value = "A"
        ElseIf score >= 80 Then
            cell.Offset(0, 1).Value = "B"
        ElseIf score >= 70 Then
            cell.Offset(0, 1).Value = "C"
        ElseIf score >= 60 Then
            cell.Offset(0, 1).Value = "D"
        Else
            If score >= 50 Then
                cell.Offset(0, 1).Value = "E"
            Else
                cell.Offset(0, 1).Value = "F"
            End If
        End If
    Next cell
End Sub

Remove Extra End Ifs

  • Run this code and the error will be resolved.

End If without Block If Error Fixed


Method 3 – Insert End If for Each If Statement

You might face End If without Block If error if an If statement does not end with an End If statement. The second If statement is missing the End If statement.

Sub AllEndIfs()
 If Range("C5").Value > 40 Then
        Range("D5").Value = "Pass"
    End If
 If Range("C6").Value > 40 Then
        Range("D6").Value = "Pass"
End Sub

End If Missing

  • The Block If without End If error will occur when you execute the code.

Block If without End If error Due to Missing End If

Solution:

  • Add an End If statement for the second If statement.
Sub AllEndIfs()
 If Range("C5").Value > 40 Then
        Range("D5").Value = "Pass"
    End If
 If Range("C6").Value > 40 Then
        Range("D6").Value = "Pass"
    End If
End Sub

Insert All End Ifs

  • Press F5 to run the code and you will get the result.

Block If without End If Solved


Method 4 – Delete Corresponding End If While Removing If Statement

While removing a part of the VBA code, you might forget to remove the corresponding End If statement. In the following code, the If statement is deleted, but the corresponding End If statement is not removed.

Sub ExtraEndIfs()
    Dim score As Integer
    For Each cell In Range("C5:C12")
        score = cell.Value 
        If score >= 90 Then
            cell.Offset(0, 1).Value = "A"
        ElseIf score >= 80 Then
            cell.Offset(0, 1).Value = "B"
        ElseIf score >= 70 Then
            cell.Offset(0, 1).Value = "C"
        ElseIf score >= 60 Then
            cell.Offset(0, 1).Value = "D"
        Else
End If
        End If
    Next cell
End Sub

Removing Part of the Code

  • The End If without block If error appears.

End If without Block If Error Occured

Solution:

  • Remove that extra End If.
Sub ExtraEndIfs()
    Dim score As Integer
    For Each cell In Range("C5:C12")
        score = cell.Value 
        If score >= 90 Then
            cell.Offset(0, 1).Value = "A"
        ElseIf score >= 80 Then
            cell.Offset(0, 1).Value = "B"
        ElseIf score >= 70 Then
            cell.Offset(0, 1).Value = "C"
        ElseIf score >= 60 Then
            cell.Offset(0, 1).Value = "D"
        Else
End If
    Next cell
End Sub

End If Deleted

  • Run the code, and the problem will be fixed.

Corresponding end Ifs Deleted


Things to Remember

  • Each If statement has only one End If statement.
  • When the If statement is written in one line, you don’t need an End If statement to terminate the If statement.
  • Use proper indentation for If and other statement blocks that improve visibility. This ensures unwanted syntaxes don’t cause any problems while executing the code.

Frequently Asked Questions

1. How do you end an if statement in VBA?

The If statement starts with an If, followed by a condition and a statement and then ends with the End statement.

If (Condition) Then
(Statement)
End If

2. What is runtime error 424 in VBA?

Runtime error 424 is an error in VBA that occurs when you run a code using incorrect object names. For example, if your object name is X and you are using Y in the code, it will cause a runtime 424 error.

3. What is the End If used for?

End If is used in VBA code to terminate a multiple-line If statement. It is written after the statement of the if condition is specified.

 

Get FREE Advanced Excel Exercises with Solutions!
Alif Bin Hussain
Alif Bin Hussain

Alif Bin Hussain earned a BSc in Civil Engineering from Bangladesh University of Engineering and Technology. As an engineering graduate, he has a deep passion for research and innovation. He loves to play with Excel. In his role as an Excel & VBA Content Developer at ExcelDemy, he not only solves tricky problems but also shows enthusiasm and expertise in handling tough situations with finesse, emphasizing his dedication to delivering top-notch content. He is interested in C, C++,... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo