本文介绍了'on error goto 0'和'on error goto -1'之间的区别 - VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以在VBA中找到On error goto -1和on error goto 0之间的区别?我试过google和msdn,但我没有运气。

Can anyone find the difference between 'On error goto -1' and 'on error goto 0' in VBA? I've tried google and msdn, but I've had no luck.

推荐答案

On Error GoTo 0 禁用过程中当前存在的任何错误捕获。

On Error GoTo 0 disables any error trapping currently present in the procedure.

发生错误GoTo -1 清除错误处理并将其设置为没有可以创建另一个错误陷阱的东西。

On Error GoTo -1 clears the error handling and sets it to nothing which allows you to create another error trap.

示例:On Error GoTo -1

在第一个错误出现之后,它将 GoTo ErrorFound ,然后将清除例程的错误处理并设置一个新的错误,当发现错误时,将 GoTo AnotherErrorFound

After the first error is raised, it will GoTo ErrorFound which will then clear the routine's error handling and set a new one, which will GoTo AnotherErrorFound when an error is found.

Sub OnErrorGotoMinusOneTest()

    On Error GoTo ErrorFound

    Err.Raise Number:=9999, Description:="Forced Error"

    Exit Sub

ErrorFound:

    On Error GoTo -1 'Clear the current error handling
    On Error GoTo AnotherErrorFound 'Set a new one
    Err.Raise Number:=10000, Description:="Another Forced Error"

AnotherErrorFound:

    'Code here

End Sub

示例:On Error GoTo 0

第一个错误提出,您将收到错误,因为错误处理已被禁用。

After the first error is raised, you will receive the error as error handling has been disabled.

Sub OnErrorGotoZeroTest()

    On Error GoTo 0

    Err.Raise Number:=9999, Description:="Forced Error"

End Sub

这篇关于'on error goto 0'和'on error goto -1'之间的区别 - VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 01:04