我有将单元格复制到下方单元格的宏。
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
,然后它会给我一个错误信息:运行时错误&#39; 13&#39; - 类型不匹配。在这种情况下,我想跳过它,然后继续复制行。
[
你能告诉我,请问怎么做?
非常感谢!
答案 0 :(得分:3)
选项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