If a called sub exit, how to the caller exit right away?

  • Thread starter Thread starter luvgreen
  • Start date Start date
L

luvgreen

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.
 
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
 
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
 
Back
Top