Handling Recurring VBA Errors Within Subroutine

只谈情不闲聊 提交于 2021-02-05 08:00:45

问题


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.


回答1:


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



回答2:


Please take some time to read up on how Error handling in VBA works.

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


来源:https://stackoverflow.com/questions/65063380/handling-recurring-vba-errors-within-subroutine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!