VBA - 复制行 - 如果发现错误,则继续复制单元格

VBA - copy rows - if it found error, then continue in copy cells

我有宏可以将单元格复制到下面的单元格。

Sub CopyRows2()

Dim LastRow As Long

With Worksheets("Ready to upload") ' <-- here should be the Sheet's name
    LastRow = .Cells(.Rows.Count, "AD").End(xlUp).Row ' last row in column C

For i = 2 To LastRow
If Range("AD" & i) > "" And Range("AD" & i + 1) = "" Then
    Range("AD" & i).Copy
    Range("AD" & i + 1).PasteSpecial xlPasteValues
    Application.CutCopyMode = False

Else

    End If
Next

End With

ActiveWindow.ScrollRow = 1 'scrolling the screen to the top

End Sub

它工作正常,直到它找到 #N/A,然后它会给我一个错误消息:运行-time error '13' - type mismatch.在那种情况下,我想跳过它,然后继续复制行。

[

你能告诉我怎么做吗?

非常感谢!

选项 1

最简单的方法是在您的代码中嵌入 On Error Resume Next。然后就可以了


选项 2 如果你想更专业一点,那么你可以使用这样的东西:

Sub CopyRows2()

    Dim LastRow As Long

    On Error Resume Next

    'your code

    If Err.Number = 13 Then Err.Clear
    On Error GoTo 0

End Sub

它会忽略错误13,但如果有其他错误它会告诉你,这非常有用。


选项 3 像这样检查错误:

If Not IsError(Range("AD" & i)) And Not IsError(Range("AD" & i + 1)) Then
    'embed the code here
End If