Stop Execution of Macro

seekeagle

New Member
Joined
Mar 5, 2009
Messages
23
I am aware that the execution of a macro can be stopped with the statement "Exit Sub". However, that only returns control the the calling subroutine. How do I completely stop execution of the entire macro?
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
If you mean "how do I abort the whole macro from inside a subroutine?"

I've never done that, but I imagine declaring a Public variable at the start of the main macro would set the stage for being able to do it:
Code:
Public AbortMe as Boolean
AbortMe = False

call SubMacro

If AbortMe = True Then Exit Sub

macro continues...

Then within the subroutine, if something happens that requires the subroutine to not only quit, but to also signal to the parent macro to stop, too, then set the AbortMe flag before exiting:
Code:
Sub SubMacro()

'macro does some stuff

If SomethingBadHappened = True Then
    AbortMe = True
    Exit Sub
Else
'    Do other stuff
End If
End Sub
You get the idea.
 
Upvote 0
Hi seekeagle,

Try using the VBA command "End" in the applicable section of you code.

HTH

Robert
 
Upvote 0
The End statement works fine. I was trying to Stop, End Macro, End Sub, etc. Any all I needed was "End".

Thanks,
 
Upvote 0

Forum statistics

Threads
1,214,972
Messages
6,122,530
Members
449,088
Latest member
RandomExceller01

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