简体   繁体   English

从子调用/运行子时,Excel VBA错误处理

[英]Excel VBA error handling when calling/run sub from sub

Having some issues understand what errorhandling does when you call/run sub or function from sub. 有一些问题可以理解当您从sub调用/运行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 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

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. 1.没有错误调用TestMinorSub之后,该子程序继续前进,直到它通过exit子程序退出。

2. There is an error in TestMinorSub AND TestMinorSub has an error handler. 2. TestMinorSub中存在错误,并且TestMinorSub具有错误处理程序。 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. 此函数可以消除错误,也可以使用Err.Raise将其重新引发到Testing子项。

3. There is an error in TestMinorSub and TestMinorSub doesn't have an error handler. 3. TestMinorSub中存在错误,并且TestMinorSub没有错误处理程序。 This will walk the call stack for a handler and jump back out to the Testing function which will handle it. 这将遍历用于处理程序的调用堆栈,然后跳回到将对其进行处理的Testing函数。 It will jump to the ErrorHandling label (this is the even handler for this function) where it displays the error via a messagebox. 它将跳到ErrorHandling标签(这是此函数的偶数处理程序),在此处通过消息框显示错误。 It then continues to the label ExitHandling where it will execute the remaining code before exiting the function. 然后,它继续到标签ExitHandling,在退出功能之前,它将在其中执行其余代码。

This type of code path is quite common for VBA and can get much more complex than your example. 这种类型的代码路径在VBA中很常见,并且比您的示例复杂得多。 Basically the developer is trying to trap the error and after the error execute some clean up code before the routine ends. 基本上,开发人员正在尝试捕获错误,并在错误结束后例程结束之前执行一些清理代码。

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: AnotherSub没有错误处理,因此SourceSub的错误处理开始了:

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. 结果, SourceSub没有完成,因为您可以看到未执行MsgBox "Source completed"行。

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. 要回答您的第二个问题,如果您取消注释AnotherSub的错误处理( On Error Goto Err: ), AnotherSub将处理该错误,因此, SourceSub将运行到完成,这是由显示源已完成的消息框指示的。

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. 这意味着,只要您在相关过程中处理错误,“ Exit Function或“ Exit Sub就不会起作用。

Here starts the errorhandling. 从这里开始错误处理。

  On Error GoTo ErrorHandling 

If error then go to ErrorHandling 如果有错误,请转到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": 如果有错误,则将代码转到该位置,并将运行以下行并转到“ ExitHandling”:
ErrorHandling: MsgBox Err.Description Resume ExitHandling 错误处理:MsgBox错误描述继续退出处理

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM