#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 661
Default Ending Sub

Hi-

I have a sub that calls another sub. If one condition in the second sub is
not met, how can I exit both subs? Using the exit sub command in the second
sub does not prevent the progam from returning to the first sub. Example:

Sub First
'Blabla
Second
'More Code (This is what I don't what executed)
End Sub

Sub Second
If condition= violated then exit sub (but returns to sub first)
End Sub

I just need to know how to write the if statement in Sub Second so if the
condition is true the program exits both subs.
--
Paul
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 983
Default Ending Sub

You can use "End" and it will stop the execution, but I would be carefull
with this kind of code. In perfect programming all functions and all sub
procedures should have only one entrance and one exit. By using End you
violate this rule and debugging can become more complex and flexibility will
be compromised. That having been said it will work.

A better way is to either change Sub 2 into a function which returns a value
which tells sub one if and or what to do next. Or have Sub 1 determine for
itself if the last block of code needs to be executed.

Hope this helps...

"Paul" wrote:

Hi-

I have a sub that calls another sub. If one condition in the second sub is
not met, how can I exit both subs? Using the exit sub command in the second
sub does not prevent the progam from returning to the first sub. Example:

Sub First
'Blabla
Second
'More Code (This is what I don't what executed)
End Sub

Sub Second
If condition= violated then exit sub (but returns to sub first)
End Sub

I just need to know how to write the if statement in Sub Second so if the
condition is true the program exits both subs.
--
Paul

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 599
Default Ending Sub

Paul

You can pass a variable ByRef and check that. Here's how I might implement
that

Sub First()

Dim bSecond As Boolean

'do stuff
Second bSecond

If bSecond Then
'do other stuff
End If

End Sub

Sub Second(ByRef bSuccess As Boolean)

bSuccess = True 'set to true to start

'something goes wrong
bSuccess = False

End Sub

--
Dick Kusleika
MVP - Excel
Excel Blog - Daily Dose of Excel
www.dicks-blog.com

"Paul" wrote in message
...
Hi-

I have a sub that calls another sub. If one condition in the second sub is
not met, how can I exit both subs? Using the exit sub command in the

second
sub does not prevent the progam from returning to the first sub. Example:

Sub First
'Blabla
Second
'More Code (This is what I don't what executed)
End Sub

Sub Second
If condition= violated then exit sub (but returns to sub first)
End Sub

I just need to know how to write the if statement in Sub Second so if the
condition is true the program exits both subs.
--
Paul



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,327
Default Ending Sub

Hi Paul

A sub is nothing but a function returning nothing (Void in other programming
languages). A function is a function (or s a sub) returning anything but
nothing. Try something like

Sub FirstMacro()
MsgBox "Hi"
If SecondMacro = True Then
MsgBox "3some"
End If
End Sub

Private Function SecondMacro() As Boolean
MsgBox "Hi 2"
'whatever then something like
If Second(Time) Mod 2 = 1 Then SecondMacro = True
End Function

Note that Second is a reserved VB function, coinsidentially matching a
suitable demo test here, so I renamed the function to avoid conflict. Also,
it's private, otherwise it would show up in Excel's curtom wosksheet
functions list.

HTH. Best wishes Harald

"Paul" skrev i melding
...
Hi-

I have a sub that calls another sub. If one condition in the second sub is
not met, how can I exit both subs? Using the exit sub command in the

second
sub does not prevent the progam from returning to the first sub. Example:

Sub First
'Blabla
Second
'More Code (This is what I don't what executed)
End Sub

Sub Second
If condition= violated then exit sub (but returns to sub first)
End Sub

I just need to know how to write the if statement in Sub Second so if the
condition is true the program exits both subs.
--
Paul



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
Filenames ending with :1 and :2 djn Excel Discussion (Misc queries) 7 January 14th 10 04:01 PM
Ending a Sheet nabanco Excel Worksheet Functions 2 June 24th 07 01:23 AM
Week ending [email protected] Excel Worksheet Functions 3 November 1st 05 04:52 PM
Ending zero RAB Excel Discussion (Misc queries) 3 August 25th 05 06:09 PM
Ending a routine art Excel Programming 1 October 27th 03 10:54 AM


All times are GMT +1. The time now is 01:03 AM.

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"