Excel VBA error handling when calling/run sub from

2019-09-05 01:58发布

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

3条回答
戒情不戒烟
2楼-- · 2019-09-05 02:38

To answer the first question, take a look at the following code snippet. There is no error handling in AnotherSub and therefore SourceSub's error handling kicks in:

Sub SourceSub()

On Error GoTo Err:

Call AnotherSub

MsgBox "Source completed"
Exit Sub

Err:

MsgBox "An error occurred in source"

End Sub

Sub AnotherSub()

'On Error GoTo Err:

MsgBox Application.WorksheetFunction.Match("1", "abcd", 0)
Exit Sub

Err:

MsgBox "An error occurred in ANotherSub"

End Sub

As a result, SourceSub does not complete as you can see that the line MsgBox "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 or Exit Sub does not make a difference as long as you handle the error in the relevant procedure.

查看更多
疯言疯语
3楼-- · 2019-09-05 02:47

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.

查看更多
小情绪 Triste *
4楼-- · 2019-09-05 02:49

Here starts the errorhandling.

  On Error GoTo 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"

ExitHandling:
  Application.ScreenUpdating = True
  Application.Calculation = xlCalculationAutomatic
  Application.EnableEvents = True
  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

End Sub
查看更多
登录 后发表回答