Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,327
Default 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



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 48
Default 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

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 863
Default 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

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 48
Default 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



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 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

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Data collation killing me!! rhani111 Excel Worksheet Functions 19 August 10th 06 02:13 AM
VBA loop is killing me! Dave Bailey Excel Discussion (Misc queries) 1 October 14th 05 07:43 AM
help me out with this - it's killing me.. popovsky[_8_] Excel Programming 6 July 22nd 05 09:20 AM
killing a sub from within an if statement Cliff L Excel Programming 8 May 11th 05 02:04 PM
Killing Excel :) Nicolas Mainczyk Excel Programming 1 October 8th 03 03:57 PM


All times are GMT +1. The time now is 10:59 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"