"on error goto"错误处理程序后,如何恢复到所需的代码行而不是恢复下一个



如何恢复到所需的代码行而不是接下来恢复。
假设我想恢复到:
cells(i,1),value = Mid(cells(i,1).value,16,Len(cells(i,1))-16)

你会如何编码?
谢谢!!

Sub testing()
    Dim i As Integer
    Dim lastrow As Integer
    lastrow = Cells(Rows.Count, "A").End(xlUp).Row
        For i = 2 To lastrow - 1          
        Cells(i, 1).Value = Mid(Cells(i, 1).Value, 16, Len(Cells(i, 1)) - 16)
        On Error GoTo errhandler_2
        Cells(i, 2).Value = Left(Cells(i, 2), Len(Cells(i, 2)) - 1)
        Cells(i, 3).Value = Left(Cells(i, 3), Len(Cells(i, 3)) - 1)
        Cells(i, 4).Value = Left(Cells(i, 4), Len(Cells(i, 4)) - 1)
    Next
    errhandler_2: Cells(i, 2).Value = "#NA"
    errhandler_3: Cells(i, 3).Value = "#NA"
    errhandler_4: Cells(i, 4).Value = "#NA"
    Resume Next
End Sub
Sub testing()
Dim i As Integer
Dim lastrow As Integer
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    For i = 2 To lastrow
letsdothis:
    Cells(i, 1).Value = Mid(Cells(i, 1).Value, 16, Len(Cells(i, 1)) - 16)
    On Error GoTo errhandler
    Cells(i, 2).Value = Left(Cells(i, 2), Len(Cells(i, 2)) - 1)
    Cells(i, 3).Value = Left(Cells(i, 3), Len(Cells(i, 3)) - 1)
    Cells(i, 4).Value = Left(Cells(i, 4), Len(Cells(i, 4)) - 1)
Next
Exit Sub
errhandler:     Cells(i, 2).Value = "#NA"
                Cells(i, 3).Value = "#NA"
                Cells(i, 4).Value = "#NA"
                i = i + 1                                  'TO PREVENT TRAP IN THE LOOP
Resume letsdothis:
End Sub

最新更新