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

后端 未结 4 2174
傲寒
傲寒 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:41

    Follow-up to paxdiablo's accepted answer. This is possible, allowing two error traps in the same sub, one after the other :

    Public Sub test()
        On Error GoTo Err1:
        Debug.Print 1 / 0
        ' more code
    Err1:
        On Error GoTo -1     ' clears the active error handler
        On Error GoTo Err2:  ' .. so we can set up another
        Debug.Print 1 / 0
        ' more code
    Err2:
        MsgBox "Got here safely"
    End Sub
    

    Using On Error GoTo -1 cancels the active error handler and allows another to be set up (and err.clear doesn't do this!). Whether this is a good idea or not is left as an exercise for the reader, but it works!

提交回复
热议问题