Difference between "Exit Sub" and "End" in a SINGLE module

bluto32

New Member
Joined
Jan 5, 2011
Messages
37
I have Googled a number of threads explaining the difference between "Exit Sub" and "End", and understand that the former only exits the current sub-procedure (returning immediately to the calling procedure), whereas the latter suddenly stops all code completely and suddenly.

But if there is no calling procedure, is there a difference between the two? For example, suppose a module has only one procedure:

VBA Code:
Sub Test()
    Dim s As String
    
    s = InputBox("Surname?")
    If s = "" Then
        Debug.Print "No name input."
        Exit Sub
    Else
        'Do some stuff
    End If
End Sub

Imagine this "Test" procedure is never, ever going to be called from anywhere else. If the "Exit Sub" is replaced with "End", the code seems to have exactly the same effect, since there is no calling procedure. But does "Exit Sub" have any hidden advantages here? Is it still a "cleaner" way to stop the code?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
If you have Global variables they will be destroyed by using End.
In the main it's best to avoid using End.
 
Upvote 0
Solution
Thanks for the swift reply.
I have adjusted my code so that s is now a global variable:

VBA Code:
Option Explicit

Dim s As String

Sub Test()
    s = InputBox("Name?")
    If s = "Popeye" Then
        Debug.Print "Oh no!!!"
        Exit Sub
    Else
        'Do some stuff
    End If
End Sub

If I enter "Popeye", the code stops at the Exit Sub. But there is no sign of the global variable string still existing. Hovering the mouse cursor over s in the code doesn't do anything; nor is there any mention of s in the variables window of the VBA editor. i.e. It looks as though "Exit Sub" has also destroyed the global variables in the case where there is no calling procedure.
 
Upvote 0
You would need to set up a watch for the variable as it will not appear in the Locals window.
 
Upvote 0
Thank you! I have never set up a "watch" before (since I typically only use local variables), and have now witnessed the behaviour you describe.
End does indeed kill the global variables.

However, I have also noticed some inconsistent behaviour with regard to how Exit Sub and End Sub treat global variables, as in the following code:

VBA Code:
Option Explicit

Dim s As String

Sub Test()
    debug.print s
    s = InputBox("Name?")
    If s = "Popeye" Then
        Debug.Print "Oh no!!!"
        Exit Sub
    Else
        'Do some stuff
    End If
End Sub

I set a watch for s and run the code. The first line prints nothing (empty string) as expected. I enter "Olive" for the name, and the code terminates normally with End Sub. I can see that s is set to "Olive" in the watch window, and yet typing debug.print s in the intermediate window gives me the empty string... But upon rerunning the code (without resetting first via the stop button), the very first line debug.print s does indeed show that s is still "Olive".

I get the same inconsistent behaviour if I type in "Popeye", so that the code ends with Exit Sub. I can see that s is set to "Popeye" in the watch window, but typing debug.print s in the intermediate window yields nothing. Rerunning the code then yields "Popeye" at the very first command.

Why does the intermediate window not remember the global variable?
 
Upvote 0
I don't know why the immediate window does not print the variable, but I'm guessing it's because there is not context.
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,269
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top