Excel VBA: On Error Goto statement not working inside For-Loop

后端 未结 4 2167
傲寒
傲寒 2020-12-06 00:04

I\'m trying to cycle through a table in excel. The first three columns of this table have text headings, the rest of them have dates as headings. I want to assign those date

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-06 00:57

    Clearing all property settings of the Err object is not the same as resetting the error handler.

    Try this:

    Sub TestErr()
    Dim i As Integer
    Dim x As Double
        On Error GoTo NextLoop
        For i = 1 To 2
    10:     x = i / 0
    NextLoop:
            If Err <> 0 Then
                Err.Clear
                Debug.Print "Cleared i=" & i
            End If
        Next
    End Sub
    

    You'll notice the just like the OP, it will catch the error properly when i =1 but it will fail on line 10 when i = 2, even though we used Err.Clear

提交回复
热议问题