When creating macros in VBA, there are often situations where you want to stop processing midway if specific conditions are met. Common examples include “a required file is not found” or “a mandatory input cell is blank.”
VBA provides Exit Sub and End as statements to interrupt processing. However, there is a clear difference between the two, and using them incorrectly can lead to unintended results.
This article explains the functions of Exit Sub and End and when to use each.
1. Exit Sub: Exits Only the Current Procedure
Exit Sub exits only the currently running Sub procedure (macro) midway and returns control to the calling procedure (if one exists).
Sample Code
In this example, the main macro MainProcess calls a helper macro CheckPrerequisites. If the condition in CheckPrerequisites is not met, Exit Sub terminates only the helper macro, and processing returns to the main macro.
' Main processing
Sub MainProcess()
' Call the helper macro
Call CheckPrerequisites
' If CheckPrerequisites ends (even via Exit Sub), this line will still run
MsgBox "Executing main process.", vbInformation
End Sub
' Helper macro for pre-checks
Sub CheckPrerequisites()
' If cell D2 is empty, exit this procedure only
If IsEmpty(Range("D2").Value) Then
MsgBox "Processing interrupted because the name is missing in cell D2.", vbExclamation
Exit Sub
End If
' If cell D2 has a value, this line runs
Debug.Print "Pre-check complete."
End Sub
Explanation:
If cell D2 is empty, CheckPrerequisites ends with Exit Sub. However, the caller MainProcess is not interrupted and attempts to execute the next line (MsgBox “Executing main process.”).
2. End: Forcefully Terminates All VBA Programs
The End statement is much more powerful. It completely stops all running VBA programs on the spot. Regardless of which procedure calls it, all processing terminates immediately.
Sample Code
Let’s replace Exit Sub from the previous code with End.
' Main processing
Sub MainProcessWithEnd()
' Call the helper macro
Call CheckPrerequisitesWithEnd
' If End is executed in the helper macro, this line will NEVER run
MsgBox "Executing main process.", vbInformation
End Sub
' Helper macro using End to stop VBA entirely
Sub CheckPrerequisitesWithEnd()
If IsEmpty(Range("D2").Value) Then
MsgBox "Fatal Error: Name missing. Terminating all processing.", vbCritical
End ' Completely stops VBA execution here
End If
End Sub
Explanation:
If cell D2 is empty, the moment End is executed within CheckPrerequisitesWithEnd, all VBA programs stop. Therefore, control does not return to the caller MainProcessWithEnd, and the message “Executing main process.” is never displayed.
Summary of Usage
| Item | Exit Sub | End |
| Scope of Effect | Only the current procedure | All VBA programs |
| Operation | Interrupts processing and returns to the caller | Completely stops processing on the spot |
| Main Use Case | – Normal exit in conditional branching – Interrupting auxiliary processing | – Occurrence of irrecoverable errors – Emergency program stop |
End is a very powerful command and is generally rarely used because it might skip object cleanup processes (such as releasing variables from memory).
In most cases, using Exit Sub (or Exit Function) to control the flow for each procedure results in a safer and more structured program. Use End only in limited situations where a fatal error occurs, and you absolutely want to stop the entire program immediately.
