VBA Exit IF

- Written by Puneet

In VBA, when you use the IF statement, you can use a GoTo statement to Exit the IF. Let me clarify here; there’s no separate exit statement that you can use with IF to exit. So, it would be best if you used goto to jump out of the IF before the line of the end statement reached.

Let’s look at an example to understand this.

vba-exit-if

In the above example, as you can see, we have used an IF statement to check if there’s a value in cell A1 or not. When you run this code, and there’s no value in the A1, VBA will jump to the “Lable1” and run the code under it. So, it will show an input box to enter the value in cell A1.

Sub myMacro()

If Range("A1") = "" Then
 GoTo Lable1
Else
 MsgBox "there's a value in the cell."
End If

Lable1:
Range("A1").Value = _
InputBox("Enter Value")

End Sub

There will be a few situations when you need to exit an IF statement while writing a VBA code. As you know in the single IF statement, there can only be two conditions, and if one condition is true, VBA will run the code that you have mentioned for it and then exit the statement automatically.