HomeVBA TutorialVBA Exit IF

In VBA, when you use the IF statement, you can use a GoTo statement to Exit the IF. Let me clear 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 reach.

Let’s look at an example to understand this.

In the above example, as you can see, we have used an IF statement to check if there’s 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
 MsgBox "there's a value in the cell."
End If

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

End Sub

Note: 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.

There’s More

Below are some of the links which could be useful for you and if you want to learn more about VBA, check out this Excel VBA Tutorial – The Best Way to Learn Programming in Excel.

VBA IF And | VBA IF Or | VBA IF Not | VBA Nested IF