在“ on error goto”错误处理程序之后,我如何恢复到所需的代码行,而不是下一个继续

时间:2019-06-21 03:49:22

标签: excel vba

如何恢复到所需的代码行,而不是下一步。
假设我想恢复到以下位置:
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

1 个答案:

答案 0 :(得分:-1)

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