Exit Main Sub from within Internal Sun
Exit Main Sub from within Internal Sun
(OP)
Hi, I'm quite new to VBA.
I have a main sub which calls other subs in order to run. If a condition in one of these Subs is met I want to exit the main sub. Using 'Exit Sub' just exits the internal sub rather than the whole program. I remember seeing how to do this somewhere a while back but can't remember where and can not find it again now. So my question is how do I do this?
I have a main sub which calls other subs in order to run. If a condition in one of these Subs is met I want to exit the main sub. Using 'Exit Sub' just exits the internal sub rather than the whole program. I remember seeing how to do this somewhere a while back but can't remember where and can not find it again now. So my question is how do I do this?





RE: Exit Main Sub from within Internal Sun
RE: Exit Main Sub from within Internal Sun
or if you don't want to do it that way, just set IERROR=0 right before the call to the subordinate subroutine, pass IERROR to this subordinate subroutine, then when condition X is met, set IERROR=1, Exit Sub, then have the If statement that stops the main sub when IERROR=1. (the reason I like to use a system of error codes is I like to print out when the program stopped, and why it stopped)
RE: Exit Main Sub from within Internal Sun
RE: Exit Main Sub from within Internal Sun
CODE
Call MySub1
Call MySub2
Call MySub3
End Sub
Sub MySub1()
'Do some stuff
End Sub
Sub MySub2()
'Do some different stuff
'where you want to quit
'the program altogether
'depending on conditions
End Sub
Sub MySub3()
'Do some stuff that you don't
'want to do if something
'happens in MySub2
End Sub
you would change to something like
CODE
Call MySub1
If MyFxn2 Then '<-This line actually calls MyFxn2 and returns True or False
Call MySub3
'and however much other stuff you want to do
End If
End Sub
Sub MySub1()
'Do some stuff
End Sub
Function MyFxn2() As Boolean
'Do some different stuff
'where you want to quit
'the program altogether
'depending on conditions
If True Then '<-Here is your condition
MyFxn2 = True 'Return a value of True
Else
MyFxn2 = False 'Return a value of False
End If
End Function
Sub MySub3()
'Do some stuff that you don't
'want to do if MyFxn2
'returns "False"
End Sub
RE: Exit Main Sub from within Internal Sun
I think for this program I will use prost's method as there would be a fair amount of code in the function's If and End If and I think the IERROR method will look neater. But that has got me thinking of different ways to solve problems so thanks
RE: Exit Main Sub from within Internal Sun
RE: Exit Main Sub from within Internal Sun
RE: Exit Main Sub from within Internal Sun
CODE
Sub myMain()
Routine = 2
On Error GoTo ExitMain
Call one
Call two
Call three
ExitMain:
End Sub
Sub one()
If Routine = 1 Then Err.Raise 513, "Sub One"
End Sub
Sub two()
If Routine = 2 Then Err.Raise 514, "Sub Two"
End Sub
Sub three()
If Routine = 3 Then Err.Raise 515, "Sub Three"
End Sub
RE: Exit Main Sub from within Internal Sun
End
ck1999