How to remove block if without end if error in Excel VBA
In this tutorial we’ll learn why the “block if without end if” error occurs in VBA and how to fix this error through simple modifications in code.
Reasons for block if without end if Error
This error usually occurs due to the following very simple reason;
An IF statement is used without a corresponding End IF statement. A multiline IF statement must terminate with a matching End IF statement.
Let’s dive into an example scenario and discuss the solutions for this error by following the steps mentioned below.
IF statement without and End IF
Let’s look at the code in this example above. It checks if the value in cell B2 is greater than 100 then write in cell B5 that the price is greater than 100. However, we can see that it has resulted in an error.
Let’s fix this error by making appropriate amendments in the code.
Microsoft Office has provided programmers the liberty of automating their tasks through Visual Basic for Office which is the official programming language for Microsoft Office Applications. While working with logical decision statements i.e., IF ELSE in VBA, we can encounter many errors and one of the very common errors is “block if without end if” error.
Step 1 – Change the code to remove error
– As mentioned earlier the root cause of the error was not closing the If statement properly. Now we’ll rectify the error by changing the code and closing the If statement properly. We’ll run the code again to see that the error has been resolved successfully.