Having some issues understand what errorhandling does when you call/run sub or function from sub.
If no errorhandling in called sub, does the errorhandling from source sub apply?
If errorhandling in called sub has "exit sub" or "Exit Function" will the source sub continue to run?
asd asd
Sub Testing()
On Error GoTo ErrorHandling
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Call TestMinorSub
ExitHandling:
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Exit Sub
ErrorHandling:
MsgBox Err.Description
Resume ExitHandling
End Sub
To answer the first question, take a look at the following code snippet. There is no error handling in
AnotherSub
and thereforeSourceSub
's error handling kicks in:As a result,
SourceSub
does not complete as you can see that the lineMsgBox "Source completed"
is not executed.To answer your second question, if you uncomment the error handling from
AnotherSub
(On Error Goto Err:
)AnotherSub
will handle the error and as a result,SourceSub
will run to completion indicated by the fact the message box showing source completed is reached.This means that the
Exit Function
orExit Sub
does not make a difference as long as you handle the error in the relevant procedure.Unstructured Excetion Handling - as Microsoft defines it (as opposed to Structured Exception Handling) isn't the best thing, and can get quite convoluted.
So in this example, if the sub is invoked from some call site...
1. No Errors After calling TestMinorSub the sub continues on its way until it exits via exit sub.
2. There is an error in TestMinorSub AND TestMinorSub has an error handler. This will handle the error according to how its defined in this function. This function can either swallow the error or raise it back to Testing sub using Err.Raise.
3. There is an error in TestMinorSub and TestMinorSub doesn't have an error handler. This will walk the call stack for a handler and jump back out to the Testing function which will handle it. It will jump to the ErrorHandling label (this is the even handler for this function) where it displays the error via a messagebox. It then continues to the label ExitHandling where it will execute the remaining code before exiting the function.
This type of code path is quite common for VBA and can get much more complex than your example. Basically the developer is trying to trap the error and after the error execute some clean up code before the routine ends.
Here starts the errorhandling.
If error then go to ErrorHandling
If there is no error the next code wil run. If there is no error the sub will exit before "Exit Sub"
If there was an error the code goea to this place and will run these lines and go to "ExitHandling":
ErrorHandling: MsgBox Err.Description Resume ExitHandling