Yes. In Excel VBA, use Exit Sub to leave the entire procedure from inside an If block, or use Exit Function to leave a function. VBA has no command that exits only the current If block; if you want to skip the remaining statements in that block but continue after its End If, structure the condition with Else or put the work in a separate procedure.
What “exit an If statement” means in VBA
An If...Then block is a way to choose which statements run. It is not a loop or a procedure, so VBA provides no Exit If statement. The word “exit” can mean two different things: stop running the current procedure altogether, or stop doing work in one branch and carry on with later code. Picking the right control flow matters, because Exit Sub and Exit Function end more than just the conditional block.
For example, in a macro, a validation check may find that a required worksheet is missing. If there is no useful work to do after that, the procedure can display a message and use Exit Sub. If the procedure must continue with other tasks, use an alternative branch or arrange the code so the later tasks are outside the conditional.
Use Exit Sub to stop a macro
Place Exit Sub inside the branch where the procedure should stop. The following example checks whether cell A1 on the active sheet is blank before continuing. Before running any macro, save a copy of the workbook: macro changes cannot be undone with Ctrl+Z.
Sub CheckInput()
' Stop if the required input cell is blank.
If Len(Trim$(CStr(ActiveSheet.Range("A1").Value))) = 0 Then
MsgBox "Enter a value in cell A1 before continuing."
Exit Sub
End If
' This runs only when A1 contains a value.
MsgBox "Input received."
End Sub
When A1 is blank or contains only spaces, the message appears and the procedure ends. When it contains a value, execution reaches the second message. The End If closes the conditional; Exit Sub, not End If, is what terminates the macro.
In Excel for Microsoft 365 on Windows, open the Visual Basic Editor with Alt+F11, then insert or select a standard module to add a macro like this. On a Mac, use Option+F11 or Fn+Option+F11, depending on the keyboard, to open the editor. Excel for the web cannot create, edit, or run VBA macros; open the workbook in the desktop application instead.
Use Exit Function when returning from a function
A function can also stop early, but it must use Exit Function. This is useful when a function detects an invalid input or has already assigned its return value. In VBA, a function returns its result through a variable with the same name as the function.
Function DiscountedPrice(ByVal price As Double, ByVal discountRate As Double) As Double
' Return zero for an invalid price or discount rate.
If price < 0 Or discountRate < 0 Or discountRate > 1 Then
DiscountedPrice = 0
Exit Function
End If
DiscountedPrice = price * (1 - discountRate)
End Function
For a price of 100 and a discount rate of 0.2, the function returns 80. For an input outside the stated limits, it assigns 0 and exits before reaching the calculation. Choose a return value that makes sense for the function’s purpose; silently returning zero can be misleading if zero could also be a valid result.
Skip the rest of a branch without ending the procedure
If the goal is to avoid running some statements while allowing the procedure to continue afterward, use an Else branch. This is the simplest substitute for exiting an If block:
Sub ProcessRow()
' Skip row-specific work when the key cell is blank.
If Len(Trim$(CStr(ActiveSheet.Range("A2").Value))) = 0 Then
MsgBox "Row 2 has no key."
Else
' Put the work that requires a key in this branch.
ActiveSheet.Range("B2").Value = "Processed"
End If
' This runs whether or not A2 contains a key.
MsgBox "Check complete."
End Sub
Here, the procedure always reaches “Check complete.” The row-specific action runs only when A2 is not blank. This keeps the control flow explicit and avoids ending the whole macro just to bypass one operation.
Another option is to reverse the condition and place the work inside an If block only when it is allowed. For several conditions, nested If blocks or a clear sequence of checks may be easier to read than a deeply nested structure. VBA does not support a general-purpose Continue If statement either.
Use a helper procedure for a clean early return
When a procedure contains substantial work, a small helper Sub can make an early-exit pattern easier to follow. The calling procedure can decide whether to continue, while the helper performs the conditional task:
Sub UpdateIfReady()
' Do the update only when the required cell is filled in.
If Len(Trim$(CStr(ActiveSheet.Range("A1").Value))) > 0 Then
WriteStatus
End If
' Other steps can continue here.
MsgBox "Main procedure finished."
End Sub
Private Sub WriteStatus()
' This procedure ends when its own work is complete.
ActiveSheet.Range("B1").Value = "Ready"
End Sub
This separation is especially useful when the conditional work has multiple steps or is reused elsewhere. The helper’s normal end returns control to the calling procedure. You can also use Exit Sub inside the helper to stop that helper, but it does not automatically end the caller.
Common mistakes and control-flow differences
- Looking for
Exit If: VBA does not provide it. Close the block withEnd If; use branches or a helper procedure to control what happens next. - Using the wrong exit command:
Exit Subbelongs in a Sub procedure, andExit Functionbelongs in a Function. They stop the current procedure, not just the current branch. - Confusing a procedure exit with a loop exit:
Exit ForandExit Doend their respective loops. If a loop is inside an If block, those commands leave the loop, then execution continues after the loop unless other control flow says otherwise. - Using
Endas a shortcut: The VBA statementEndstops execution abruptly and can reset the project’s state. For ordinary early returns, use the appropriateExit SuborExit Function. - Assuming
Andprevents an unsafe second check: VBA does not short-circuit Boolean expressions. Both sides of anAndexpression can be evaluated, so use separate nested checks when the second expression depends on the first being safe.
These distinctions also help when debugging. If code after an If does not run, check whether an earlier Exit Sub or Exit Function is reached, and whether the branch condition is true. Indenting each block consistently makes it much easier to see which statements an exit will bypass.
Frequently Asked Questions
Does VBA have an Exit If statement?
No. VBA uses End If to close an If block, but there is no Exit If command. Use Else, restructure the condition, or move work into a separate procedure.
Does Exit Sub leave only the If block?
No. It ends the current Sub procedure immediately. Statements later in that procedure do not run, including statements after the End If.
How do I exit an If inside a For loop?
To stop the entire procedure, use Exit Sub or Exit Function. To stop only the loop, use Exit For. To skip the rest of the current loop pass and continue with the next one, VBA has no direct Continue For statement; restructure the loop body using an If condition.
What should a VBA function use to return early?
Use Exit Function, after assigning an appropriate value to the function name if the caller expects a result. Otherwise, the return value may not clearly indicate what happened.
Can I run VBA macros in Excel for the web?
No. Excel for the web can open a macro-enabled workbook, but VBA macros cannot be created, edited, or run there. Use desktop Excel; the exact shortcut to open the editor differs on Mac.
Related
- Excel AND Statement for Conditional Formatting Rules
- How to Exit Track Changes in Word Without Losing Edits
- Google Sheets IF Statement Color: Change Cell Colors
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- VBA Function List: Built-In Functions, Groups, and UseA VBA function list is a reference to built-in functions you can call from Visual Basic for Applications,…
- Call of Duty Macros for Controller: How They WorkCall of Duty macros for controller are input sequences—like rapid-fire, drop-shotting, or jump-shotting—that are automated and triggered by…
- Excel UserForm Layout: Design, Controls, and Setup TipsAn Excel UserForm layout is the arrangement of the form window and its controls—such as text boxes, labels,…
- Excel VBA: Select a Range Using VariablesIn Excel VBA, use variables inside Cells(row, column) and pass the two cell references to Range to build…
- Macros Settings Windows 10: How to Find and Use ThemIn Windows 10, “macros settings” usually refers to the Macro Settings pane in Microsoft Office (Word, Excel, PowerPoint)…
- Excel Macro Named Range: How to Create and Use OneAn Excel macro named range is a descriptive name, such as SalesAmounts, that VBA can assign to a…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.