Else Without If Error in VBA: Why Is it Happening?
What you’re seeing is a compile error that indicates that an Else (or ElseIf) keyword was not preceded by a correct If statement.
Meaning, the compiler found an Else statement (which it will highlight for the user) without seeing an If statement in the lines above it. This is an error because it violates the correct syntax of an If-Then-Else statement. The correct syntax is as follows:
For using the shorthand syntax for if statement that specifies a condition and an action (for when the condition is met):
If
[Test Expression] Then
[Action]
To use the standard syntax for an If statement that specifies a condition and an action (for when the condition is met) and an alternate action for when the condition is not met:
If
[Test Expression] Then
[Action]
Else
[Alternate Action]
End if
For specifying more than one condition (and their actions):
If
[Test Expression] Then
[Action]
ElseIf
[Test Expression 2] Then
[Action 2]
Else
[Alternate Action]
End if
The compiling error “Else Without If” occurs when “If
[Test Expression] Then
“ is missing or written incorrectly. Let’s discuss the common causes of the error further in details below with examples.
Contents
Missing If Statement
When VBA compiler sees that the Else keyword is not preceded by a recognizable (correct) If statement, it generates an error ‘Else without If’. For every Else, there must be an If statement. However, for every If, we do not necessarily need an else statement.
Example 1: Missing If Statement
In this example, we display an input box that takes the user’s input and stores it in x. Then we encounter an Else keyword, but there was no If statement prior to it. This indeed will cause a compiler error: Else Without If.
Sub Else_Without_If() x = InputBox("Set x value")' 'if statement should go here, but it’s missing. This will cause a compile error: Else without If MsgBox = "x value is equal to 1" Else MsgBox = "x value is not equal to 1" End If End Sub
To solve this problem, we just need to add an If statement. An If statement consists of If
[condition] Then
.
Sub Else_Without_If() x = InputBox("Set x value") If x = 1 Then MsgBox = "x value is equal to 1" Else MsgBox = "x value is not equal to 1" End If End Sub
Following good indentation practices is crucial for recognizing whether each if-else-then logic consists of the necessary keywords (If statement, Else keyword, End If keyword).
If
[condition] Then
[action]
Else
If
[condition] Then
[action]
Else
[action]
End If
[action]
End If
Example 2: Else statement is inside a loop and If statement is outside
You might be surprised that you are getting the error when you already have the If statement present in the code and placed prior to the Else keyword and written correctly. The problem might not be apparent at first glance. But if we look closer, we will see that the scope of the If statement is different from that of the else statement.
Everything between the If statement and the End If keyword belongs to the scope of this If statement. Other scopes can also exist in the code due to the existence of loops such as For Next loop, Do Until loop or For Each Next loop.
A scope exists when a logical statement requires multiple keywords to indicate the start and end points of the statement. A problem occurs if you overlap scopes in a way that causes one to be partially placed inside the other. The correct way to have multiple scopes work with each other is to have one totally placed inside the other.
In the following example, If
[condition] Then
is followed by the beginning of a For
loop followed by the Else
keyword. This separates the If statement and the Else keyword and causes the error to show up.
Sub Else_Without_If() x = InputBox("Set x value") 'starting the if statement scope If x = 1 Then MsgBox "x value is equal to 1" ‘starting a new scope (For Next loop) For R = 1 To 5 'Else keyword is separated from its If statement because it’s in a different scope. Else Next R MsgBox "x value is not equal to 1" End If End Sub
To fix the issue, we ensure that the If logical statement as a whole is fully encompassed within the For loop. Or the For loop is fully encompassed within the If logical statement Action; between If and Else or between Else and End If.
As a general rule, if a section of your code has a some status due to having a starting and ending point (If – Else, Else – End If, For – Next, Do – Until) then you cannot start another section (scope) within it without ending it within the first section.
Start Point (If, Else, For, Do)
Another Start Point
Do Something
Another End Point
End Point (Else, End If, Next, Until)
To apply this to our example, we change it to the following code;
Sub Else_Without_If() x = InputBox("Set x value") 'starting the if statement scope If x = 1 Then MsgBox "x value is equal to 1" 'starting a new scope (For Next loop) For R = 1 To 5 MsgBox "x value is equal to 1" 'ending the new scope within the same scope it was started in Next R Else MsgBox "x value is not equal to 1" End If End Sub
Incorrect If Statement
Example 3: Placing the action on the same line with If Statement
Another very common mistake that is often made is to write the If statement in a way that is not compatible with utilizing the Else keyword.
If we want to use the Else keyword, we must place the action part of the If-Then-Else logic in the next line after the if statement. If we are not using the Else keyword, then we can place the action on the same line as the If statement and we do not write the Else keyword or the End If keyword.
When the action (that should be carried out if the condition is true) is placed on the same line as the If statement, then the If statement is considered complete. This is considered a shorthand way of writing the If – then logic.
That is why when compiler encounters an Else keyword after that, it does not find an If statement that belongs to the Else keyword, because that If statement has been completed with placing the action on the line as the If statement. The following code is NOT correct and will cause the compiler error: Else without If.
Sub Else_Without_If() x = InputBox("Set x value") If x = 1 Then MsgBox "x value is equal to 1" Else MsgBox "x value is not equal to 1" End If End Sub
The action (MsgBox "x value is equal to 1"
) needs to be placed on the next row after the If statement in order to be able to have an Else statement used in the If-Then-Else logic. In the following code, we moved in the action statement to the next line, which fixes the problem.
Sub Else_Without_If() x = InputBox("Set x value") If x = 1 Then MsgBox "x value is equal to 1" Else MsgBox "x value is not equal to 1" End If End Sub
We now have covered all the possible causes of compile error: Else without if. To recap, if you get this error, check whether
1) There is an If statement in place that precedes the Else keyword.
2) The if statement line does not contain anything other than If
[condition] Then
.
3) Verify that the If statement and the Else keyword are not separated by another scope or section, such as loops.