Exiting a Sub when called from another Sub

R

RyanH

My codes is writen in a way that if a conditon is met in Sub1 I want to exit
ExitSub, is this possible?

Sub ExitSub()

Call Sub1
Call Sub2
Call Sub3

End Sub
 
N

Nigel

Use functions instead of Sub1, Sub2 etc., then the Function return value
can be set True if you need to exit.

Sub ExitSub
If Function1 then ExitSub
If Function2 then ExitSub
End Sub

Function Function1() As Boolean

' set return to True if you want to Exit main sub
Function1 = True

End Function
 
M

Mike H

Hi,

This passes the variable x between subs. If x is set to 1 then ExitSub
terminates. For example in the code below ExitSub terminates after Sub1 has
run.
BTW no need for Call

Public x
Sub ExitSub()
x = 0
sub1
If x = 1 Then Exit Sub
sub2
sub3

End Sub

Sub sub1()
x = 1
MsgBox "In sub1"
End Sub

Sub sub2()
MsgBox "In sub2"
End Sub

Sub sub3()
MsgBox "In sub3"
End Sub
 
D

Don Guillett

You did NOT post your code for comments as you ALWAYS should. Here is an
example where you could put em together. A select case would probably be
even better.

Sub exitif()
Call subone
'called after sub one is over
Call subtwo
End Sub

Sub subone()
For Each c In Range("h1:h13")
MsgBox c
If c = 15 Then
MsgBox "found it"
Exit Sub
End If
Next c
End Sub
Sub subtwo()
For Each c In Range("h1:h13")
MsgBox c
If c = 14 Then
MsgBox "found it"
Exit Sub
End If
Next c
End Sub

Sub putemtogether()
For Each c In Range("h1:h13")
MsgBox c
If c = 15 Then
MsgBox "found it"
Exit Sub
End If

If c = 14 Then
MsgBox "found it"
Exit Sub
End If

Next c
End Sub
 

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