A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Excel » General Discussion
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Exit a Procedure from a Sub within a sub



 
 
Thread Tools Display Modes
  #1  
Old June 25th, 2008, 06:02 PM posted to microsoft.public.excel.misc
[email protected]
external usenet poster
 
Posts: 138
Default Exit a Procedure from a Sub within a sub

I'm running a procedure that calls other macros. In one of these macros I
have an if then statement that if true I want to exit the entire procedure.
What is the code for this? Exit Sub just exits the current sub I'm running,
not the entire larger one.

Thanks

Adam Bush
  #2  
Old June 25th, 2008, 06:13 PM posted to microsoft.public.excel.misc
Steve
external usenet poster
 
Posts: 2,662
Default Exit a Procedure from a Sub within a sub

How about "End"

" wrote:

I'm running a procedure that calls other macros. In one of these macros I
have an if then statement that if true I want to exit the entire procedure.
What is the code for this? Exit Sub just exits the current sub I'm running,
not the entire larger one.

Thanks

Adam Bush

  #3  
Old June 25th, 2008, 06:14 PM posted to microsoft.public.excel.misc
Charles Williams
external usenet poster
 
Posts: 235
Default Exit a Procedure from a Sub within a sub

Sub MainSub()
Dim blQuit as boolean

blQuit=False
SubSub blQuit

if blQuit then exit sub

....
End Sub
Sub SubSub(blQuit as boolean)

'''
If thereisanerror then
blQuit=true
exit sub
endif
....
End Sub

Charles
__________________________________________________
The Excel Calculation Site
http://www.decisionmodels.com

"
m wrote in message
...
I'm running a procedure that calls other macros. In one of these macros I
have an if then statement that if true I want to exit the entire
procedure.
What is the code for this? Exit Sub just exits the current sub I'm
running,
not the entire larger one.

Thanks

Adam Bush



  #4  
Old June 25th, 2008, 06:18 PM posted to microsoft.public.excel.misc
Dave Peterson
external usenet poster
 
Posts: 19,791
Default Exit a Procedure from a Sub within a sub

You could change the called sub to a function and return a boolean value that
you could check after the call.

Sub SubA()
if Functa() = false then
exit sub
end if
End sub
Function FunctA()

functa = true 'be positive!
if somethingbadhappens then
functa = false
exit function 'leave here immediately, too
end if
End Function

Or you could use a module level (or public) variable and just have your called
sub change its status.

Dim OkToContinue as boolean
sub suba()
call subb
if oktocontinue = false then
exit sub
end if
...
End sub
sub subb()
oktocontinue = false
end sub

wrote:

I'm running a procedure that calls other macros. In one of these macros I
have an if then statement that if true I want to exit the entire procedure.
What is the code for this? Exit Sub just exits the current sub I'm running,
not the entire larger one.

Thanks

Adam Bush


--

Dave Peterson
  #5  
Old June 25th, 2008, 06:24 PM posted to microsoft.public.excel.misc
Charles Williams
external usenet poster
 
Posts: 235
Default Exit a Procedure from a Sub within a sub

Usually using End is bad practice as it resets all your VBA code and
variables without going through any exit routines.


Charles
__________________________________________________
The Excel Calculation Site
http://www.decisionmodels.com

"Steve" wrote in message
...
How about "End"

" wrote:

I'm running a procedure that calls other macros. In one of these macros
I
have an if then statement that if true I want to exit the entire
procedure.
What is the code for this? Exit Sub just exits the current sub I'm
running,
not the entire larger one.

Thanks

Adam Bush



  #6  
Old June 25th, 2008, 06:59 PM posted to microsoft.public.excel.misc
Bob Phillips[_2_]
external usenet poster
 
Posts: 1,562
Default Exit a Procedure from a Sub within a sub

Do it as functions with some error testing,

Global Const AppErrorNum As Long = 19999
Global ErrorMsg As String

Sub Main()

Const ProcName as String = "Main"

On Error Goto Main_Error

'some code

If Not MyFirstCall Then Err.Raise AppErrorNum

'some more code

If Not MySecondCall Then Err.Raise AppErrorNum
'etc

Main_Exit:
Exit Sub

Main_Error:
If ErrorMsg = "" Then
ErrorMsg = "Error in " & ProcName & vbNewLine & _
"Error: " & Err.Number & ", " & Err.Description
End If
MsgBox ErrorMsg
Resume Main_Exit
End Sub

Public Sub MyFirstCall() As Boolean

Const ProcName as String = "MyFirstCall "
MyFirstCall = True
On Error Goto MyFirstCall_Error

' the real code

MyFirstCall_Exit:
'tidy-up code
Exit Function

MyFirstCall_Error:
MyFirstCall = False
ErrorMsg = "Error in " & ProcName & vbNewLine & _
"Error: " & Err.Number & ", " & Err.Description
Resume MyFirstCall_Exit
Exit Function

Public Sub MySecondCall() As Boolean

Const ProcName as String = "MySecondCall"
MySecondCall = True
On Error Goto MySecondCall_Error

' the real code, including

If Not MyThirdCall Then Err.Raise AppErrorNum

MySecondCall_Exit:
'tidy-up code
Exit Function

MySecondCall_Error:
MySecondCall = False
ErrorMsg = "Error in " & ProcName & vbNewLine & _
"Error: " & Err.Number & ", " & Err.Description
Resume MySecondCall_Exit
Exit Function

Public Sub MyThirdCall() As Boolean

Const ProcName as String = "MyThirdCall"
MyThirdCall = True
On Error Goto MyThirdCall_Error

' the real code

MyThirdCall_Exit:
'tidy-up code
Exit Function

MyThirdCall_Error:
MyThirdCall= False
ErrorMsg = "Error in " & ProcName & vbNewLine & _
"Error: " & Err.Number & ", " & Err.Description
Resume MyThirdCall_Exit
Exit Function

--
__________________________________
HTH

Bob

"
m wrote in message
...
I'm running a procedure that calls other macros. In one of these macros I
have an if then statement that if true I want to exit the entire
procedure.
What is the code for this? Exit Sub just exits the current sub I'm
running,
not the entire larger one.

Thanks

Adam Bush



 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 03:35 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.