ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   killing all procedures? (https://www.excelbanter.com/excel-programming/341897-killing-all-procedures.html)

Ouka[_24_]

killing all procedures?
 

Hi all,

How do you kill all procedures on an error?

I have a procedure that calls a series of other procedures. I have
error code in each of those sub procedures that will kill that sub
procedure if there is an error within that sub procedure, but the main
procedure thread still keeps firing off.




Code:
--------------------
Private Sub MasterList_click()

'procedure code here'

Call SubP1
Call SubP2

End sub


Private SubP1()

On Error Goto Handler1
Exit Sub
Handler1:
MsbBox "An error has occured, exiting procedure"

'sub procedure code here'

End Sub
--------------------



But SubP2 still fires off, as well as anything else downstream of SubP1
in MasterList even if SubP1 errors out and is killed properly.

So what can I insert into the error handler in SubP1 that will kill all
procedures entirely?


--
Ouka
------------------------------------------------------------------------
Ouka's Profile: http://www.excelforum.com/member.php...o&userid=23988
View this thread: http://www.excelforum.com/showthread...hreadid=473148


Harald Staff

killing all procedures?
 
You want the Sub to return something. That's a Function, not a Sub. Or
rather; a Sub is a Fubctionthat returns nothing. Try this, testrun
MasterList_click a few times until it makes sense:

Private Sub MasterList_click()
If P1 = False Then Exit Sub
If P2 = False Then Exit Sub
'and so on
End Sub

Private Function P1() As Boolean
Dim R As Long
On Error GoTo Handler1
R = 2 / (Second(Now) Mod 2)
P1 = True
MsgBox "Yo from P1"
Exit Function
Handler1:
P1 = False
MsgBox Error
End Function

Private Function P2() As Boolean
MsgBox "Yo from P2!"
End Function

HTH. Best wishes Harald


"Ouka" skrev i melding
...

Hi all,

How do you kill all procedures on an error?

I have a procedure that calls a series of other procedures. I have
error code in each of those sub procedures that will kill that sub
procedure if there is an error within that sub procedure, but the main
procedure thread still keeps firing off.




Code:
--------------------
Private Sub MasterList_click()

'procedure code here'

Call SubP1
Call SubP2

End sub


Private SubP1()

On Error Goto Handler1
Exit Sub
Handler1:
MsbBox "An error has occured, exiting procedure"

'sub procedure code here'

End Sub
--------------------



But SubP2 still fires off, as well as anything else downstream of SubP1
in MasterList even if SubP1 errors out and is killed properly.

So what can I insert into the error handler in SubP1 that will kill all
procedures entirely?


--
Ouka
------------------------------------------------------------------------
Ouka's Profile:

http://www.excelforum.com/member.php...o&userid=23988
View this thread: http://www.excelforum.com/showthread...hreadid=473148




Walt[_3_]

killing all procedures?
 
Hi Ouka,

From the Excel VBA Help system:

Statement: End
Description: Terminates execution immediately. Never required by
itself but may be placed anywhere in a procedure to end code execution,
close files opened with the Open statement and to clear variables.

Meaning this should do it:
Private SubP1()
On Error Goto Handler1
'sub procedure code here'
Exit Sub
Handler1:
MsbBox "An error has occurred, exiting procedure"
'EXIT code here, if any, besides the following End statement'
End <<<<<<<<< This shuts down VBA execution at this point
End Sub

NOTE: The closing of VBA opened files and clearing of variables may
require accommodation in your program flow.

Best Regards,
Walt


Myrna Larson

killing all procedures?
 
True, but not considered "good programming practice" by the purists, and it
has the drawback you note re open files, etc.

On 4 Oct 2005 15:41:24 -0700, "Walt" wrote:

Hi Ouka,

From the Excel VBA Help system:

Statement: End
Description: Terminates execution immediately. Never required by
itself but may be placed anywhere in a procedure to end code execution,
close files opened with the Open statement and to clear variables.

Meaning this should do it:
Private SubP1()
On Error Goto Handler1
'sub procedure code here'
Exit Sub
Handler1:
MsbBox "An error has occurred, exiting procedure"
'EXIT code here, if any, besides the following End statement'
End <<<<<<<<< This shuts down VBA execution at this point
End Sub

NOTE: The closing of VBA opened files and clearing of variables may
require accommodation in your program flow.

Best Regards,
Walt


Walt[_3_]

killing all procedures?
 
Hi Myrna,

I try to never say never, but there's usually a way to avoid the
necessity of violating a best practice - point taken. Given that Ouka
wants his main to quit on failure in SubP1, would something like this
be better?:

'Declare a public variable
Public OKGo as Boolean

'The Main
Private Sub MasterList_click()
'procedure code here'
OKGo = True
Call SubP1
If OKGo then Call SubP2
End sub

'The First Sub
Private SubP1()
On Error Goto Handler1
'sub procedure code here'
Exit Sub
Handler1:
MsbBox "An error has occurred, exiting procedure SubP1"
'EXIT code here, if any, besides the following OKGo setting'
OKGo = False
End Sub

Best Regards,
Walt


Ouka[_25_]

killing all procedures?
 

Thanks all! Both ways worked like a charm. Just trying to decide whic
result I like better

--
Ouk
-----------------------------------------------------------------------
Ouka's Profile: http://www.excelforum.com/member.php...fo&userid=2398
View this thread: http://www.excelforum.com/showthread.php?threadid=47314



All times are GMT +1. The time now is 06:58 PM.

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