Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 586
Default Stop Procedure when an Error occurs in another procedure

Can you stop Procedure A from contiueing to call other procedure if there is
an error in procedure C?

Sub ProcedureA()

Call ProcedureB
Cal ProcedureC
Call ProcedureD
Call ProcedureE

Ens Sub
--
Cheers,
Ryan
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Stop Procedure when an Error occurs in another procedure

You can use On Error to trap the error. Something like this...

Sub ProcedureA()
On Error GoTo SomethingWentWrong
Call ProcedureB
Cal ProcedureC
Call ProcedureD
Call ProcedureE
Exit Sub
SomethingWentWrong:
' Any clean up code you might need to execute
End Sub

A couple of points to note (although you should look up the On Error
Statement in the help files for comprehensive details)... Use On Error GoTo
and then specify the name of a code label (SomethingWentWrong in the above
example) and then provide a section of code below your main code that starts
with that label name (same naming rules as for variables except that all
code labels require a colon at the end of their name... clues VB into the
fact that it is a label and not the name of a subroutine). After the code
label, you can place any code that you might need to "clean things up"
before exiting you subroutine (or function). The Exit Sub statement
immediately above the code label name is needed to prevent your main code's
execution from "falling through" into your error handling code.

--
Rick (MVP - Excel)


"RyanH" wrote in message
...
Can you stop Procedure A from contiueing to call other procedure if there
is
an error in procedure C?

Sub ProcedureA()

Call ProcedureB
Cal ProcedureC
Call ProcedureD
Call ProcedureE

Ens Sub
--
Cheers,
Ryan


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 586
Default Stop Procedure when an Error occurs in another procedure

I actually already have that type of structure. But under each procedure I
have I have an Error Handler. For example,

Sub ProcedureA()
On Error GoTo ErrorHandler
Call ProcedureB
Call ProcedureC
Call ProcedureD
Call ProcedureE
Exit Sub
'***********
ErrorHandler:
Call ErrorHandler(ModuleName, SubName)

End Sub

Sub ProcedureB()
On Error GoTo ErrorHandler
' procedure B code here
Exit Sub

'****************
ErrorHandler:
Call ErrorHandler(ModuleName, SubName)

End Sub

Sub ErrorHandler(ModuleName As String, SubName As String)
Msgbox "Something went wrong in " & ModuleName & ", " & SubName
End Sub

I have an On Error GoTo ErrorHandler for each procedure because I want the
Msgbox to display the module name and procedure name the error came from. In
your example I will not beable to return the SubName.

I was really wanting to know if there is a line of code that I could put in
the ErrorHandler Procedure to kill the entire macro? Instead of it only
canceling Procedure B and continuing on with the other procedures.

--
Cheers,
Ryan


"Rick Rothstein" wrote:

You can use On Error to trap the error. Something like this...

Sub ProcedureA()
On Error GoTo SomethingWentWrong
Call ProcedureB
Cal ProcedureC
Call ProcedureD
Call ProcedureE
Exit Sub
SomethingWentWrong:
' Any clean up code you might need to execute
End Sub

A couple of points to note (although you should look up the On Error
Statement in the help files for comprehensive details)... Use On Error GoTo
and then specify the name of a code label (SomethingWentWrong in the above
example) and then provide a section of code below your main code that starts
with that label name (same naming rules as for variables except that all
code labels require a colon at the end of their name... clues VB into the
fact that it is a label and not the name of a subroutine). After the code
label, you can place any code that you might need to "clean things up"
before exiting you subroutine (or function). The Exit Sub statement
immediately above the code label name is needed to prevent your main code's
execution from "falling through" into your error handling code.

--
Rick (MVP - Excel)


"RyanH" wrote in message
...
Can you stop Procedure A from contiueing to call other procedure if there
is
an error in procedure C?

Sub ProcedureA()

Call ProcedureB
Cal ProcedureC
Call ProcedureD
Call ProcedureE

Ens Sub
--
Cheers,
Ryan



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Stop Procedure when an Error occurs in another procedure

Did you really expect us to figure out the structure you have now described
to us from the description you provided in your original posting?

The problem you are facing is that each subroutine has its own error
handler, so that is where the error is handled at... so no, you can't
control ProcedureA's execution from within them. However, if those other
procedures (ProcedureB, ProcedureC, etc.) did not have any error handlers,
then the error handler in ProcedureA would process the error. Try this.
Comment out each On Error GoTo statement in ProcedureB, ProcedureC, etc. (so
there is effectively no error handling taking place in the individually
called procedures) and use something like this for ProcedureA and see if it
does what you want...

Sub ProcedureA()
Dim ModuleName As String
Dim SubName As String
ModuleName = "Module1"
On Error GoTo SomethingWentWrong
SubName = "ProcedureB"
Call ProcedureB
SubName = "ProcedureC"
Call ProcedureC
SubName = "ProcedureD"
Call ProcedureD
SubName = "ProcedureE"
Call ProcedureE
Exit Sub
SomethingWentWrong:
MsgBox "Something went wrong in " & ModuleName & ", " & SubName
End Sub

--
Rick (MVP - Excel)


"RyanH" wrote in message
...
I actually already have that type of structure. But under each procedure I
have I have an Error Handler. For example,

Sub ProcedureA()
On Error GoTo ErrorHandler
Call ProcedureB
Call ProcedureC
Call ProcedureD
Call ProcedureE
Exit Sub
'***********
ErrorHandler:
Call ErrorHandler(ModuleName, SubName)

End Sub

Sub ProcedureB()
On Error GoTo ErrorHandler
' procedure B code here
Exit Sub

'****************
ErrorHandler:
Call ErrorHandler(ModuleName, SubName)

End Sub

Sub ErrorHandler(ModuleName As String, SubName As String)
Msgbox "Something went wrong in " & ModuleName & ", " & SubName
End Sub

I have an On Error GoTo ErrorHandler for each procedure because I want the
Msgbox to display the module name and procedure name the error came from.
In
your example I will not beable to return the SubName.

I was really wanting to know if there is a line of code that I could put
in
the ErrorHandler Procedure to kill the entire macro? Instead of it only
canceling Procedure B and continuing on with the other procedures.

--
Cheers,
Ryan


"Rick Rothstein" wrote:

You can use On Error to trap the error. Something like this...

Sub ProcedureA()
On Error GoTo SomethingWentWrong
Call ProcedureB
Cal ProcedureC
Call ProcedureD
Call ProcedureE
Exit Sub
SomethingWentWrong:
' Any clean up code you might need to execute
End Sub

A couple of points to note (although you should look up the On Error
Statement in the help files for comprehensive details)... Use On Error
GoTo
and then specify the name of a code label (SomethingWentWrong in the
above
example) and then provide a section of code below your main code that
starts
with that label name (same naming rules as for variables except that all
code labels require a colon at the end of their name... clues VB into the
fact that it is a label and not the name of a subroutine). After the code
label, you can place any code that you might need to "clean things up"
before exiting you subroutine (or function). The Exit Sub statement
immediately above the code label name is needed to prevent your main
code's
execution from "falling through" into your error handling code.

--
Rick (MVP - Excel)


"RyanH" wrote in message
...
Can you stop Procedure A from contiueing to call other procedure if
there
is
an error in procedure C?

Sub ProcedureA()

Call ProcedureB
Cal ProcedureC
Call ProcedureD
Call ProcedureE

Ens Sub
--
Cheers,
Ryan




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 586
Default Stop Procedure when an Error occurs in another procedure

Sorry about that, I know your not a mind reader. Your explanation of the
error handling will work nicely, Thanks!
--
Cheers,
Ryan


"Rick Rothstein" wrote:

Did you really expect us to figure out the structure you have now described
to us from the description you provided in your original posting?

The problem you are facing is that each subroutine has its own error
handler, so that is where the error is handled at... so no, you can't
control ProcedureA's execution from within them. However, if those other
procedures (ProcedureB, ProcedureC, etc.) did not have any error handlers,
then the error handler in ProcedureA would process the error. Try this.
Comment out each On Error GoTo statement in ProcedureB, ProcedureC, etc. (so
there is effectively no error handling taking place in the individually
called procedures) and use something like this for ProcedureA and see if it
does what you want...

Sub ProcedureA()
Dim ModuleName As String
Dim SubName As String
ModuleName = "Module1"
On Error GoTo SomethingWentWrong
SubName = "ProcedureB"
Call ProcedureB
SubName = "ProcedureC"
Call ProcedureC
SubName = "ProcedureD"
Call ProcedureD
SubName = "ProcedureE"
Call ProcedureE
Exit Sub
SomethingWentWrong:
MsgBox "Something went wrong in " & ModuleName & ", " & SubName
End Sub

--
Rick (MVP - Excel)


"RyanH" wrote in message
...
I actually already have that type of structure. But under each procedure I
have I have an Error Handler. For example,

Sub ProcedureA()
On Error GoTo ErrorHandler
Call ProcedureB
Call ProcedureC
Call ProcedureD
Call ProcedureE
Exit Sub
'***********
ErrorHandler:
Call ErrorHandler(ModuleName, SubName)

End Sub

Sub ProcedureB()
On Error GoTo ErrorHandler
' procedure B code here
Exit Sub

'****************
ErrorHandler:
Call ErrorHandler(ModuleName, SubName)

End Sub

Sub ErrorHandler(ModuleName As String, SubName As String)
Msgbox "Something went wrong in " & ModuleName & ", " & SubName
End Sub

I have an On Error GoTo ErrorHandler for each procedure because I want the
Msgbox to display the module name and procedure name the error came from.
In
your example I will not beable to return the SubName.

I was really wanting to know if there is a line of code that I could put
in
the ErrorHandler Procedure to kill the entire macro? Instead of it only
canceling Procedure B and continuing on with the other procedures.

--
Cheers,
Ryan


"Rick Rothstein" wrote:

You can use On Error to trap the error. Something like this...

Sub ProcedureA()
On Error GoTo SomethingWentWrong
Call ProcedureB
Cal ProcedureC
Call ProcedureD
Call ProcedureE
Exit Sub
SomethingWentWrong:
' Any clean up code you might need to execute
End Sub

A couple of points to note (although you should look up the On Error
Statement in the help files for comprehensive details)... Use On Error
GoTo
and then specify the name of a code label (SomethingWentWrong in the
above
example) and then provide a section of code below your main code that
starts
with that label name (same naming rules as for variables except that all
code labels require a colon at the end of their name... clues VB into the
fact that it is a label and not the name of a subroutine). After the code
label, you can place any code that you might need to "clean things up"
before exiting you subroutine (or function). The Exit Sub statement
immediately above the code label name is needed to prevent your main
code's
execution from "falling through" into your error handling code.

--
Rick (MVP - Excel)


"RyanH" wrote in message
...
Can you stop Procedure A from contiueing to call other procedure if
there
is
an error in procedure C?

Sub ProcedureA()

Call ProcedureB
Cal ProcedureC
Call ProcedureD
Call ProcedureE

Ens Sub
--
Cheers,
Ryan




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
Stop a Procedure from another procedure Ayo Excel Discussion (Misc queries) 1 October 30th 08 01:42 AM
stop automatic recalulation when error occurs DagL Excel Programming 1 October 11th 07 11:52 AM
How to jump from a Form procedure to a Workbook or Module procedure? T. Erkson Excel Programming 4 January 25th 07 07:15 PM
Values assigned to variables staying in memory when procedure stop Peter Rooney Excel Programming 4 August 12th 05 03:13 PM


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

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

About Us

"It's about Microsoft Excel"