Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Stop a Procedure from another procedure | Excel Discussion (Misc queries) | |||
stop automatic recalulation when error occurs | Excel Programming | |||
How to jump from a Form procedure to a Workbook or Module procedure? | Excel Programming | |||
Values assigned to variables staying in memory when procedure stop | Excel Programming |