How to Fix ‘Else Without If’ Error in VBA (5 Simple Ways)

If you see the “Else without If” error in VBA, it usually happens when an Else statement has no matching If, or when your If…Then…Else block is incomplete. This can stop your macro from running and show the compile error message.

In this article, we will walk you through why VBA shows the “Else without If” error and how to fix it, whether it’s caused by a missing If statement, a single-line If with Else, a missing End If, an extra End If, or an additional End If after deleting an If statement. By following these steps, you can ensure your code runs correctly and shows the intended results.

Key Takeaways

Steps to fix “Else Without If” error caused by a missing If statement:

➤ Open the VBA editor and locate the code showing the error
➤ Check that every Else has a matching If above it
➤ Use a proper block If … Then … Else … End If structure
➤ Run the code again, and it will run with errors.

overview image

Download Practice Workbook
1

Add the Missing If Statement

The most common reason behind the “Else without If” error in VBA is a missing If statement. Suppose you write Else without a corresponding If, Excel cannot process the code. So, if your code is showing Else Without If error in VBA, first check whether every Else has a matching If. You need to ensure that each Else is properly paired with an If to prevent this error and make your VBA code run.

Here is a sample dataset that we will use to demonstrate the method.

Add the Missing If Statement

Steps:

➤ Open the VBA editor
➤ Insert the following VBA code in the VBA module.

Sub ElseWithoutIfErrorRange()
    Dim c As Range
    For Each c In Range("C2:C13")
        ' If statement is missing
        Else
            c.Offset(0, 1).Value = "Fail"
    Next c
End Sub

➤ Here, the code has an Else without a matching If.

Add the Missing If Statement

➤ Run the code, and Excel shows “Else without If”.

Add the Missing If Statement

➤ Add the missing If statement and run the code again.

Sub ElseWithoutIfFixedRange()
    Dim c As Range
    For Each c In Range("C2:C13")
        If c.Value >= 50 Then
            c.Offset(0, 1).Value = "Pass"
        Else
            c.Offset(0, 1).Value = "Fail"
        End If
    Next c
End Sub

Add the Missing If Statement

➤ Now the code will work properly without showing the compile error and will show the pass fail results based on the scores of the students.

Add the Missing If Statement


2

Avoid Single-Line If Statements With Else

In VBA, you can use single-line If statements, but they do not allow an Else part. If you try to combine both on one line, VBA shows the “Else without If” error. That is why you have to always use a block If … Then … Else … End If structure when your logic requires both outcomes.

Steps:

➤ For example, insert the following VBA code in the VBA editor.

Sub PassFail_SingleLineError()
    Dim c As Range
    For Each c In Range("C2:C13")
        If c.Value >= 50 Then c.Offset(0, 1).Value = "Pass"
        Else
            c.Offset(0, 1).Value = "Fail"
    Next c
End Sub

➤ Here in this code, a single-line If statement is tried to continue with Else on the next line. In single-line If statements, Else must be on the same line as Then or removed.

Avoid Single-Line If Statements With Else

➤ If you run the code, it will show the error message Else Without If.

Avoid Single-Line If Statements With Else

➤ Remove Else from the single-line If statements and run the code again.

Sub PassFail_SingleLineFixed()
    Dim c As Range
    For Each c In Range("C2:C13")
        If c.Value >= 50 Then c.Offset(0, 1).Value = "Pass" Else c.Offset(0, 1).Value = "Fail"
    Next c
End Sub

Avoid Single-Line If Statements With Else

➤ Now the code will run without showing a compile error and show the pass fail results based on the score of the students.

Avoid Single-Line If Statements With Else


3

Add Missing End If Statements

The error in VBA usually occurs when you don’t close an If block with the End If statement. Every If that spans multiple lines must have a corresponding End If. In case the If … Else block was not properly closed with an End If, Excel cannot recognize where the block ends and shows an error.

Steps:

➤ For example, to see how the method works, insert the following VBA code in the VBA editor.

Sub PassFail_MissingEndIf()
    If Range("C2").Value >= 50 Then
        Range("D2").Value = "Pass"
    End If
    If Range("C3").Value >= 50 Then
        Range("D3").Value = "Pass"
    ' <-- End If missing here
End Sub

➤ Here, the first If block is fine because it’s properly closed with End If.
➤ The second If block is missing its End If. That is why Excel doesn’t know where the block finishes.

Add Missing End If Statements

➤ Run this code, and Excel will show the compile error: “Block If without End If

Add Missing End If Statements

➤ Now add the missing End If before the Eng Sub line

Sub PassFail_AllEndIfsFixed()
    If Range("C2").Value >= 50 Then
        Range("D2").Value = "Pass"
    End If
    If Range("C3").Value >= 50 Then
        Range("D3").Value = "Pass"
    End If
End Sub

Add Missing End If Statements

➤ Run the code again. Now the code will work properly and will show the Pass or Fail based on the student’s score.

Add Missing End If Statements


4

Delete Extra End If Statements

The “Else without If” error in VBA often appears if you mistakenly add an extra End If statement. In those cases, VBA expects each If block to have only one matching End If. That is why, in the VBA code, if you insert more than required, Excel cannot match them properly and throws an error.

Steps:

➤ For example, open the VBA editor and paste the following code:

Sub PassFail_ExtraEndIf()
    Dim c As Range
    For Each c In Range("C2", Cells(Rows.Count, "C").End(xlUp))
        If c.Value >= 50 Then
            c.Offset(0, 1).Value = "Pass"
        Else
            c.Offset(0, 1).Value = "Fail"
        End If
        End If   ' <-- EXTRA End If (this single line causes the compile error)
    Next c
End Sub

➤ Here, there are two End If statements.
➤ The first End If correctly closes the If … Else block.
➤ The second End If has no matching If above it.

Delete Extra End If Statements

➤ Run the code, and Excel will show the error End If without block If.

Delete Extra End If Statements

➤ Now, delete the extra End If at the bottom and run the code again.

Sub PassFail_Fixed()
    Dim c As Range
    For Each c In Range("C2", Cells(Rows.Count, "C").End(xlUp))
        If c.Value >= 50 Then
            c.Offset(0, 1).Value = "Pass"
        Else
            c.Offset(0, 1).Value = "Fail"
        End If
    Next c
End Sub

➤ Now the code works properly, displaying Pass or Fail based on the student’s score.


5

Remove End If When Deleting an If Statement

Sometimes, the “Else without If” error in VBA appears if you delete an If … Then statement but forget to remove its corresponding End If. VBA expects every End If to have a matching If. If the opening If is missing, Excel cannot match them properly and shows the error.

Steps:

➤ For example, open the VBA editor and paste the following code.

Sub PassFail_Error()
    Dim c As Range
    For Each c In Range("C2:C13")
        'If c.Value >= 50 Then    ' <-- deleted If line by mistake
            c.Offset(0, 1).Value = "Pass"
        Else
            c.Offset(0, 1).Value = "Fail"
        End If   ' <-- orphan End If (error)
    Next c
End Sub

➤ Here in this code, The If line was deleted. For this reason, the Else and End If now have no matching If.

Remove End If When Deleting an If Statement

➤ Run the code, and Excel will show the error: “Else without If”.

Remove End If When Deleting an If Statement

➤ Now, put the missing If back (or remove the orphan End If) and run the code again

Sub PassFail_Fixed()
Dim c As Range
For Each c In Range("C2:C13")
If c.Value >= 50 Then
c.Offset(0, 1).Value = "Pass"
Else
c.Offset(0, 1).Value = "Fail"
End If
Next c
End Sub

Remove End If When Deleting an If Statement

➤ Run the code again.
➤ Now the code works properly, displaying Pass or Fail based on the scores of the students.

Remove End If When Deleting an If Statement


Frequently Asked Questions

Is there an else if in VBA?

Yes, VBA supports an ElseIf statement within an If…Then…Else block. You use it when you want to test multiple conditions sequentially. Here is a general structure.

If condition1 Then
    ' Code for condition1
ElseIf condition2 Then
    ' Code for condition2
Else
    ' Code if none of the conditions are true
End If

Can we use if and else if without else?

Yes, in VBA, you can use If and ElseIf without a final Else statement. The Else part is optional. For example

If score >= 90 Then
    MsgBox "Excellent"
ElseIf score >= 75 Then
    MsgBox "Good"
End If

Here, if the score is below 75, nothing will change. You only need an Else if you want to handle all other cases.

Can you have two else if statements?

Yes, VBA allows multiple ElseIf statements within a single If…Then…Else block. You can add as many ElseIf statements as needed to cover different conditions, but only one Else is allowed at the end.


Wrapping Up

The “Else without If” error in VBA usually occurs due to a missing If statement, a single-line If with Else, a missing End If, an extra End If, or an additional End If after deleting an If. Always check that every Else has a matching If, use proper block If…Then…Else…End If structures, and remove any extra or orphan End If statements. These steps ensure your VBA code runs properly without compile errors and shows the correct results.

ExcelInsider Team
We will be happy to hear your thoughts

Leave a reply

Excel Insider
Logo