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

我有macros复制单元格到下面的单元格。

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 ,那么它会给我一个错误消息: 运行时错误'13'types不匹配 。 在这种情况下,我想跳过它,然后继续复制行。

[ 在这里输入图像描述

你能告诉我,请问怎么做?

非常感谢!

选项1

最简单的方法是在您的代码中embeddedOn 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