exit sub

M

mike allen

how do i get out of all subs upon a certain event?

sub1()
call sub2
'more code here--i do not want sub1 to get this far unless sub2 runs all the
way through
end sub

sub2()
if 1=1 then
exit sub 'i want this to quit sub1 as well as sub2
end if
end sub

there may be more embedded subs, so i don't think i can specify which sub to
exit (if that is even possible).
thanks, mike allen
 
F

Frank Stone

your example is a bit confusing. but if code stops before
a call, the call is never made. move your call to the end
of sub1(or at least after the exit sub command.) Some
where you will have to add a exit sub command in sub1 to
stop it from call sub2. now...where to put the exit sub
command. you didn't provide enought information to answer
that.
 
M

mike allen

my placements are as i want them. to clarify: i am simply running sub1,
which does certain things, then calls sub2. now in sub2, certain things are
done, then IF something happens (1=1 in this case), i want to exit
everything. exiting sub2 is easy, but i don't want it going back and
finishing sub1 (after "call sub2"), which is what it does in this example.
this may help:
sub1()
'various code is written here
call sub2
'more various code is written here, but do not want to go this far unless
sub2 is completed
end sub

sub2()
'various code is written here
if 2=2 then
exit sub 'this is where i need to exit not only sub2, but sub1 as well
endif
'more various code is written here
end sub

thanks, mike allen
 
E

Ed

Mike: The way you have your example written is a bit confusing, because it
doesn't look like there's any more code in sub1 to run after sub2 ends for
whatever reason. But assuming there is more code, can you set a boolean
True/False in sub2 and pass it to sub1? Then maybe:
sub1()
'various code is written here
call sub2
'more various code is written here, but do not want to go this far unless
sub2 is completed
***
If bolEndMe = True
GoTo Bye
End If
' more code

' just before End Sub statement
Bye:
end sub

sub2()
'various code is written here
if 2=2 then
exit sub 'this is where i need to exit not only sub2, but sub1 as well
endif
'more various code is written here
end sub

thanks, mike allen

When sub2 exits, it will drop back into sub1 right after the call. If the
boolean is set to indicate that, based on sub2, you want to end all code, it
would process the GoTo and drop you down to the End Sub.

HTH
Ed
 
H

Harald Staff

mike allen said:
how do i get out of all subs upon a certain event?

sub1()
call sub2
'more code here--i do not want sub1 to get this far unless sub2 runs all the
way through
end sub

Hi Mike

You want to evaluate the RESULT of a macro. Those macros are not Subs (which
in reality are Functions returning Void, nothing) but Functions (returning
whatever you ask them to). See if the following makes sense to you. The test
in 2 here is whether the seconds on the clock is odd or even.

Sub Main()
Call Code1
If Code2 = True Then
Call Code3
End If
End Sub

Sub Code1()
MsgBox "Hello world from 1"
End Sub

Function Code2() As Boolean
If Second(Now) Mod 2 = 0 Then Code2 = True
MsgBox Code2 & " from 2"
End Function

Sub Code3()
MsgBox "Hello world from 3"
End Sub

HTH. Best wishes Harald
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top