## How to Fix the “End If without block If" Error

Today I’ll show you how to resolve the error “End If without block If” in VBA. But first, you have to understand the “If” statement in order to fix the issue.

## The IF statement and its various forms

The If statement is a conditional clause that helps us to run code using a condition that is decided during runtime. You might wonder, “What is the need to decide the condition during runtime? Can’t we decide that earlier?” In reality, there are many situations where an action needs to be performed only if certain criteria are met or a condition is fulfilled. Sometimes this check might even depend on the user’s input value.

For example, let us imagine that a bank offers 8% ROI on fixed deposit accounts if the customer is a senior citizen and only 6% ROI for other customers. In this case, the code that calculates the interest and maturity amount should both a) consider the age of the customer and b) use a condition to use different values for senior and non-senior citizens. This is where an “If conditional statement" steps in.

Now let’s see the code for the above scenario assuming that one must be 60 years old to be called a senior citizen.

```Sub sample_coding()
'declaration of variables
Dim matamt, prinamt, roi, term, custage
‘ receive input from user
custage = InputBox("Enter the age of the customer")
‘ assign some values
prinamt = 10000 ' Principal amount
term = 2 ' 2 years
' decide the roi value
If custage &amp;lt; 60 Then
roi = 6
Else
roi = 8
End If
' formula to calculate the FD maturity amount.
matamt = prinamt + (prinamt * roi * term / 100)
‘ printing the output
Debug.Print matamt
End Sub
```

Looking at the example above, we see that the syntax for using a simple If statement is

`If <condition> Then`

`<code>`

`End If`

But the same conditional statement has different forms  as listed below.

1. A simple If Block
2. An If – Else block
3. An Else-If block
4. Nested If block

## The Compile Error “End If without Block If:

This is a simple compile time error that’s thrown when the code containing any If blocks do not comply with the syntax (or) such a statement does not exist.

Here are some instances where this error might occur

### Rule 1: End If with single line statement

If the single line of code to be executed is placed in the same line as the “If – then" statement, then the “End If" statement needs to be omitted. In other words, the If statement is considered complete without an “End If" statement in cases where the conditional code is placed in the same line.

```If &amp;lt;condition&amp;gt; Then &amp;lt;code&amp;gt;
```

For example:

The If condition in the above code can be rewritten using this rule to avoid the compile error “End if without block If".

```' Fix an roi in common
roi = 8
'Change the value for non-senior citizens alone using the rule 1
If custage &amp;lt; 60 Then roi = 6
' comment or remove the end if statement to fix the error.
'End If
```

According to Rule 1, if “End If" is used in the above code, you will encounter the error “End If without block If". So, do not forget to remove it.

### Rule 2: Extra End If statements

If you’re using nested if conditions are used, ensure that every “If" statement that has been opened, has a corresponding “End If" statement. This is in addition to Rule 1 above.

Example 1

```If custage &amp;lt; 60 Then
roi = 6
If strgen = "Female" And custage &amp;gt; 57 Then roi = 8
End If '********Line is explained below*********
Else
roi = 8
End If
```

In this piece of code,

• The inner “If" condition follows Rule 1 (i.e. code is placed in the same statement after “Then" keyword). Therefore, this statement is a standalone statement that does not require “End If".
• But since we have an “End If" statement , it will be considered to be the corresponding “End “ of the outer if statement (Line 1).
• This leads to the “Else" keyword in the fifth line looking for its corresponding “If statement". In turn, we end up with the error “Else without If" which is similar to “End if without block If".
• The solution to this problem is to remove the unnecessary “End if" in line 4 or place the code “`roi=8`" in the next line i.e between the IF… then and the End if statements.

Example 2

```If apple = "sweet" Then
If mango = "sweet" Then Debug.Print "Fruits are sweet"
End If
End If
```

In this example,

• Here since line 2 is already complete without “End if “, line 3 is automatically matched with the If statement of line number 1.
• So, the “End If" in line 4 searches for its pair of “If statement" and leads to the compile error “End if without block If".
• The solution to this is to remove line 3 or place the “`Debug.Print`" statement in a separate line before the “End If" statement in line no 3.

### Rule 3: Forgetting part of your deleted code

Ensure that there is no “End if" statement left behind without an “If" statement in your code. This might happen when you maintain code or change your logic after a long period of time.

For example, you might think that an “If – End if “ block of code might not be required in a certain place. And after you delete that “If block", you may forget to delete its “End If" statement. This again causes the same compile error we keep seeing, “End if without block If".

For Example:

```If apple = "sweet" Then
End If
End If
```

Imagine that you wanted to delete the inner If block in the above example. While doing so, you forgot to delete the “End If" statement. Then, you are sure to encounter the compile error “End If without block If".

Here is a video that explains everything outlined above with sample code.  The code is explained and executed line by line, so you can completely understand what causes the error “End if without block If".

## Summary

Basically, when you look at the error statement, it is clear that it is thrown if there are any unnecessary ‘End If’ statements. The only solution is to trace the code and remove the erroneous statement after confirming that it does not affect the rest of the code in any way.

The other compile error “Else without If", for which there is an example in Rule 2, is related to this error. It is thrown when there is an “Else <some code> End If" statement or just an “Else" statement without an “If <condition>  Then" statement. In general, for any error , it is wise and time saving to check the syntax first and then proceed with troubleshooting.