Kill Macro

J

Jeff

Hi,

I am running a macro and want to put in an option where the program stops
under certain conditions

IF (statement=true) then stop the macro

I was using "Exit Sub" but this just kicks it out of the sub(), not the
entire macro. Is there a way to end the entire macro?

Thanks for your help!
 
O

Otto Moehrbach

You do this with the statement End. That's it. It is highly recommended,
though, that you not ever use the End statement. You will be taking a
chance of leaving a lot of loose ends. Don't do it!!!!

You can back out of all the code in an orderly fashion. I do it all the
time. Post back and provide more detail about what you have. For instance,
what is the condition that will provoke you to end all code running? HTH
Otto
 
P

Pete_UK

You could use a global variable which is initially set to False, but
gets set to True by your statement (as well as Exit Sub there). Then in
all your subs you could check for the variable being True at the start
and Exit Sub if it is.

Hope this helps.

Pete
 
O

Otto Moehrbach

Jeff
For instance, let's say you have a macro like this:
Sub Macro
Call Macro2
Call Macro3
End Sub
And you want to place your IF statement in Macro2 and stop all code if True.
Do something like this:
Dim CancelA As Boolean
Sub Macro
CancelA = False
Call Macro2
If CancelA = True Then Exit Sub
Call Macro3
End Sub
Within Macro2 you would have:
If (Statement = True) Then
CancelA = True
Exit Sub
End If
HTH Otto
 
Top