Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 586
Default 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


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 735
Default 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



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default 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


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,124
Default 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



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exit Sub not exiting JHop Excel Programming 13 November 24th 07 07:54 PM
exiting the whole macro tigoda Excel Programming 9 October 1st 07 03:34 PM
exiting program PLPE[_17_] Excel Programming 2 July 18th 05 11:30 AM
Exiting Worksheet Evan Excel Discussion (Misc queries) 1 February 11th 05 06:06 PM
Exiting a workbook Dick Kusleika Excel Programming 0 August 15th 03 05:30 PM


All times are GMT +1. The time now is 11:37 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"