ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Exiting a Sub when called from another Sub (https://www.excelbanter.com/excel-programming/404694-exiting-sub-when-called-another-sub.html)

RyanH

Exiting a Sub when called from another Sub
 
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



Nigel[_2_]

Exiting a Sub when called from another Sub
 
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

--

Regards,
Nigel




"RyanH" wrote in message
...
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




Mike H

Exiting a Sub when called from another Sub
 
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

"RyanH" wrote:

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



Don Guillett

Exiting a Sub when called from another Sub
 
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

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"RyanH" wrote in message
...
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





All times are GMT +1. The time now is 04:26 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com