Exit sub but not working

hatstand

Well-known Member
Joined
Mar 17, 2005
Messages
778
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I have three pieces of code that do various stuff and are called from a button and run one after another. My problem lies in the first code. The code checks whether a file exists. If it does all is well. If it doesn't, I have used Exit Sub. But after it uses the Exit Sub the code jumps to the next piece of code and cause an error.

How do I get the code to completely stop at Exit Sub?
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try looking to use Stop, check it out first.
 
Upvote 0
I took your advice and read up on stop. I noticed that End does the same and comes out of the vba.

Thanks for the pointer. It's working as it should now.
 
Upvote 0
try an On Error Resume 0 code..

or try adding this line at the start of your macro

Code:
On Error Goto quitnow
and add this line before your end sub

Code:
quitnow:
     Exit Sub

:) cheers!
 
Upvote 0
Another option is to change the first Sub to a Function which returns a value indicating whether it was successful or not, then only run the second and third macros if it was successful.

So instead of this sort of thing:-
Code:
[FONT=Fixedsys]sub button_click()[/FONT]
[FONT=Fixedsys]  call macro1[/FONT]
[FONT=Fixedsys]  call macro2[/FONT]
[FONT=Fixedsys]  call macro3[/FONT]
[FONT=Fixedsys]end sub[/FONT]
[FONT=Fixedsys] [/FONT]
[FONT=Fixedsys]sub macro1()[/FONT]
[FONT=Fixedsys][COLOR=green]  ' do something[/COLOR][/FONT]
[FONT=Fixedsys]  if {[I]bad_condition[/I]} then end[/FONT]
[FONT=Fixedsys][COLOR=green]  ' maybe do something else[/COLOR][/FONT]
[FONT=Fixedsys]end sub[/FONT]
[FONT=Fixedsys] [/FONT]
[FONT=Fixedsys]sub macro2()[/FONT]
[FONT=Fixedsys] [COLOR=green] ' do something[/COLOR][/FONT]
[FONT=Fixedsys]end sub[/FONT]
[FONT=Fixedsys] [/FONT]
[FONT=Fixedsys]sub macro3()[/FONT]
[FONT=Fixedsys][COLOR=green]  ' do something[/COLOR][/FONT]
[FONT=Fixedsys]end sub[/FONT]
You'd do something like this:-
Code:
[FONT=Fixedsys]sub button_click()[/FONT]
[FONT=Fixedsys]  if macro1=true then[/FONT]
[FONT=Fixedsys]    call macro2[/FONT]
[FONT=Fixedsys]    call macro3[/FONT]
[FONT=Fixedsys]  endif[/FONT]
[FONT=Fixedsys]end sub[/FONT]
[FONT=Fixedsys] [/FONT]
[FONT=Fixedsys]function macro1() as boolean[/FONT]
[FONT=Fixedsys]  macro1=true [COLOR=green]' assume all is ok unless we decide otherwise[/COLOR][/FONT]
[FONT=Fixedsys][COLOR=green]  ' do something[/COLOR][/FONT]
[FONT=Fixedsys]  if {[I]bad_condition[/I]} then macro1=false[/FONT]
[FONT=Fixedsys][COLOR=green]  ' maybe do something else[/COLOR][/FONT]
[FONT=Fixedsys]end function[/FONT]
[FONT=Fixedsys] [/FONT]
[FONT=Fixedsys]sub macro2()[/FONT]
[FONT=Fixedsys][COLOR=green]  ' do something[/COLOR][/FONT]
[FONT=Fixedsys]end sub[/FONT]
[FONT=Fixedsys] [/FONT]
[FONT=Fixedsys]sub macro3()[/FONT]
[FONT=Fixedsys] [COLOR=green] ' do something[/COLOR][/FONT]
[FONT=Fixedsys]end sub[/FONT]
Get the idea?
 
Upvote 0
This produces a nice tight flow of program execution without the jumping about and other unwanted side-effects that On Error directives cause.

Generally I discourage the use of On Error unless it's genuinely very difficult or impossible to detect an error condition before it arises. I would never use it to handle completely normal and foreseeable run-time conditions.
 
Last edited:
Upvote 0
Never use End on its own in code.
Always use an error handler.
:)
 
Upvote 0
"An error handler" isn't necessarily On Error, of course. The code I posted contains an error handler.

I believe using On Error as the default method for handling conditions which are normal and foreseeable promotes sloppy thinking and poor program design.
 
Last edited:
Upvote 0
Until they add Try...Catch...Finally to VBA, I'm not sure what else you would use?
 
Upvote 0
are you calling the form from a procedure

because if you are, that procedure will have to BE MADE TO WAIT for the form to complete it's code otherwise it calls the form and continues executing code after the form call before the code in the form has finished.

public waitforform as boolean

in procedure
set waitforform=true
call form
while waitforform
doevents
wend



in the form when you've finished waitforform=false
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,728
Members
452,939
Latest member
WCrawford

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