处理子例程中的重复VBA错误 [英] Handling Recurring VBA Errors Within Subroutine

查看:46
本文介绍了处理子例程中的重复VBA错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我坚持使用一种我可以解决的方法,但是令我感到困惑的是,我没有直接的答案来解决如何使用 On Error Goto 重复发生错误的问题.我的问题与该问题基本上相同

运行 Err.Clear 后,我希望 i = 3 的行为与 i = 0 时的行为相同.我的过程因以下VBA错误而停止,人们期望它不会捕获任何错误.

我认为有某种方法可以重置 Error 变量或在没有解决方法的情况下处理这种情况?任何质量回应都将得到认可.谢谢.

解决方案

要告诉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()出现错误时GoTo fixingErrors将theArray(9)变暗,我变长对于i = 0到9'* On Error GoTo fixingErrors'下一行会故意在以下情况下产生错误:'当i = {0}时,按预期方式工作,但在i = 3时,下一行将引发错误.theArray(i)= i +1/(i Mod 3)'*出错时转到0next_i:接下来我退出子'----错误处理----'这在i = 0时按预期工作,但在i = 3,6,9时不起作用fixingErrors:'*清除错误'*出错时转到0在这一点上,我希望我的错误状态与过程开始时的状态相同吗?theArray(i)= -1继续next_i结束子 

I am stuck on something that I have a workaround for, but it bugs me I don't have a direct answer for how to address the issue of using On Error Goto for recurring errors. My question is essentially the same as this one, however the answers provided are alternatives to the OP's overall approach rather than how to handle the specific issue.

I've simplified an example below. I am WELL AWARE that there are probably a dozen ways this code could be rewritten to avoid any error -- I am just using this to illustrate the issue of trying to figure out why/how does this code work properly when i = 0, but not when 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

What my Err variable shows after first instance of 0.

After I run Err.Clear I would expect the behavior for i=3 to be the same as when i=0, however my procedure stops with the below VBA error one would expect WITHOUT any error catching.

I presume there's some way to reset the Error variable or handle this type of situation without a workaround? Any quality responses will get upvoted. Thanks.

解决方案

To tell VBA that an you have dealt with the error you need a Resume statement. Therefore, replacing the line GoTo next_i with Resume next_i you give you the outcome you expect.

You do not need Err.Clear. Also, On Error GoTo 0 will disable the error handler. However, neither of these lines will tell VBA that you have dealt with an error.

In your code at i=0 the error handler is activated but VBA is not told that the error has been dealt with (i.e. no Resume statement). At i=3 another error occurs while a previous error hadn't been dealt with. In this case the current procedure/function/property cannot deal with the second error, which is therefore, fatal.

You should also take the On Error GoTo fixingErrors line outside the loop.

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

这篇关于处理子例程中的重复VBA错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆