ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   If a called sub exit, how to the caller exit right away? (https://www.excelbanter.com/excel-programming/292397-if-called-sub-exit-how-caller-exit-right-away.html)

luvgreen[_4_]

If a called sub exit, how to the caller exit right away?
 
Good morning! I have a question to ask experts. The main sub will a sub2, if
there is error in sub2, sub2 will pop a message and exit. How can I make the
main sub exit right away without running other code behind calling sub2?
thanks.




Frank Kabel

If a called sub exit, how to the caller exit right away?
 
Hi
one way:
make your second sub a function which return FALSE or an error code. In
your main sub you can use somethin like

sub main_sub()
....
ret = subfunction2
If not ret then exit sub
....
end sub


--
Regards
Frank Kabel
Frankfurt, Germany

luvgreen wrote:
Good morning! I have a question to ask experts. The main sub will a
sub2, if there is error in sub2, sub2 will pop a message and exit.
How can I make the main sub exit right away without running other
code behind calling sub2? thanks.



Tom Ogilvy

If a called sub exit, how to the caller exit right away?
 
On way is to put the error handler in Sub1 and not in sub2.


--
Regards,
Tom Ogilvy

"luvgreen" wrote in message
...
Good morning! I have a question to ask experts. The main sub will a sub2,

if
there is error in sub2, sub2 will pop a message and exit. How can I make

the
main sub exit right away without running other code behind calling sub2?
thanks.






Chip Pearson

If a called sub exit, how to the caller exit right away?
 
You should make Sub2 a function that returns a value, and then
continue or discontinue code in MainSub based on the return value
of Sub2. E.g.,

Sub MainSub()
Dim Continue As Boolean
Continue = Sub2()
If Continue = False Then
Exit Sub
End If
End Sub

Function Sub2() As Boolean
' your code
If Something Then
Sub2 = False
Exit Function
End If
' rest of code
End Function


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com




"luvgreen" wrote in message
...
Good morning! I have a question to ask experts. The main sub

will a sub2, if
there is error in sub2, sub2 will pop a message and exit. How

can I make the
main sub exit right away without running other code behind

calling sub2?
thanks.






luvgreen[_3_]

If a called sub exit, how to the caller exit right away? - Thanks all!
 
Thank you all for your kind help. It worked.


All times are GMT +1. The time now is 10:37 AM.

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