ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Ending Sub (https://www.excelbanter.com/excel-programming/312206-ending-sub.html)

Paul

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

Jim Thomlinson[_3_]

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


Dick Kusleika[_3_]

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




Harald Staff

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





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

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