【问题标题】:Handling Recurring VBA Errors Within Subroutine处理子程序中重复出现的 VBA 错误
【发布时间】:2020-11-29 18:04:43
【问题描述】:

我被困在一些我有解决方法的问题上,但这让我很困扰我没有直接的答案来解决如何解决使用On Error Goto 处理重复出现的错误的问题。我的问题与as this one 基本相同,但提供的答案是 OP 整体方法的替代方案,而不是如何处理具体问题。

我在下面简化了一个示例。我很清楚可能有十几种方法可以重写这段代码以避免任何错误——我只是用它来说明试图弄清楚的问题为什么/如何在 i = 0 时/如何正常工作,但在 i = 3、6、9 时却不行?

Sub vbaErrorConfusion()
Dim theArray(9) As Long, i As Long

    For i = 0 To 9
        On Error GoTo fixingErrors
        'next line will intentionally create an error when
        'when i = {0} this works as expected, but at i=3 next line throws an error.
        theArray(i) = i + 1 / (i Mod 3)
        On Error GoTo 0
    
next_i:
    Next i

Exit Sub
'----Error Handling----
'this works as expected when i=0 but not when i = 3,6,9
fixingErrors:
    
    Err.Clear
    On Error GoTo 0
    'at this point I would expect my error state to be the same as the start of procedure?
    theArray(i) = -1
    GoTo next_i
End Sub

我的 Err 变量在 0 的第一个实例之后显示的内容。

在我运行Err.Clear 之后,我希望i=3 的行为与i=0 时的行为相同,但是我的程序会因以下 VBA 错误而停止,预计不会捕获任何错误。

我认为有一些方法可以重置 Error 变量或在没有解决方法的情况下处理这种情况?任何高质量的回复都会得到支持。谢谢。

【问题讨论】:

  • 下面 FreeFlow 的答案是正确的。使用 Err.Clear 只会清除 Err 对象中的文本和数字,它不会清除错误。见this。这有点反直觉。
  • 感谢@PaulKelly(又名错误陷阱之王,在他的资源丰富的页面中进行了解释:excelmacromastery.com/vba-error-handling

标签: vba error-handling


【解决方案1】:

要告诉 VBA 你已经处理了错误,你需要一个Resume 语句。因此,将行 GoTo next_i 替换为 Resume next_i 您会得到您期望的结果。

您不需要Err.Clear。此外,On Error GoTo 0 将禁用错误处理程序。但是,这两行都不会告诉 VBA 你已经处理了一个错误。

i=0 的代码中,错误处理程序已激活,但未告知 VBA 已处理错误(即没有 Resume 语句)。在i=3 发生另一个错误,而先前的错误尚未处理。在这种情况下,当前的过程/函数/属性无法处理第二个错误,因此这是致命的。

您还应该将On Error GoTo fixingErrors 放在循环之外。

Sub vbaErrorConfusion()
On Error GoTo fixingErrors
Dim theArray(9) As Long, i As Long

    For i = 0 To 9
        '*On Error GoTo fixingErrors
        'next line will intentionally create an error when
        'when i = {0} this works as expected, but at i=3 next line throws an error.
        theArray(i) = i + 1 / (i Mod 3)
        '*On Error GoTo 0
    
next_i:
    Next i

Exit Sub
'----Error Handling----
'this works as expected when i=0 but not when i = 3,6,9
fixingErrors:
    
    '*Err.Clear
    '*On Error GoTo 0
    'at this point I would expect my error state to be the same as the start of procedure?
    theArray(i) = -1
    Resume next_i
End Sub

【讨论】:

    【解决方案2】:

    请花一些时间了解 VBA 中错误处理的工作原理。

    Sub vbaErrorConfusion()
        Dim theArray(9) As Long, i As Long
    
        For i = 0 To 9
            On Error GoTo fixingErrors
            'next line will intentionally create an error when
            'when i = {0} this works as expected, but at i=3 next line throws an error.
            theArray(i) = i + 1 / (i Mod 3)
            'On Error GoTo 0
        
    'next_i:
        Next i
    
        Exit Sub
        '----Error Handling----
        'this works as expected when i=0 but not when i = 3,6,9
    fixingErrors:
        
        Err.Clear
        Debug.Print "An error was generated for i= ", i
        'On Error GoTo 0
        'at this point I would expect my error state to be the same as the start of procedure?
        theArray(i) = -1
        Resume Next
    End Sub
    

    【讨论】:

    • 谢谢,您的代码确实有效,但我喜欢其他答案提供的解释。我在这里对你的回答投了赞成票,我查看了你的其他几个答案,并点击了这篇文章,因为我认为它很有用(stackoverflow.com/questions/62994772/…)。谢谢
    猜你喜欢
    • 2015-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-23
    • 2017-10-15
    • 1970-01-01
    • 2014-02-20
    • 2014-08-21
    相关资源
    最近更新 更多