我正在尝试编写一个宏,该宏将合并2张工作表中的数据并将它们依次粘贴到新工作表中。我的代码的第二部分有问题。
我得到
应用程序或对象定义的错误。
我认为范围可能是一个问题-我想从转储表中复制D列的所有已使用行,并将它们粘贴到“摘要”表中K列中的最后一个已使用行之后。这是我到目前为止所拥有的
Sub Paste()
Dim lRow3 As Long
Dim rng3 As Range
With ThisWorkbook
With .Sheets("Dump Lease & RMP Charges")
lRow3 = .Cells(.Rows.count, 1).End(xlUp).Row
Set rng3 = .Range("D3:D" & lRow3)
rng3.Copy Destination:=ThisWorkbook.Sheets("Summary Invoice ex").Range("K6")
End With
With Sheets("Dump MMS Service and Repairs")
.Range(.Range("D3").End(xlToRight)).Copy 'line with error
End With
With Sheets("Summary Invoice ex")
.Cells(.Rows.count, "K").End(xlUp).Offset(1, 0).PasteSpecial
End With
End With
End Sub
我将最后一部分更改为
With .Sheets("Dump MMS Service and Repairs")
lRow4 = .Cells(.Rows.count, 1).End(xlUp).Row
Set rng4 = .Range("D3:D" & lRow4)
rng4.Copy Destination:=ThisWorkbook.Sheets("Summary Invoice ex").Cells(lRow4 + 1, "K")
End With
现在没有错误,但是没有粘贴!
答案 0 :(得分:1)
要复制从D3单元格传出的D列中所有已使用的行,请尝试
.Range(.Cells(.Rows.Count, "D").End(xlUp), .Cells(3, .Columns.Count).End(xlToLeft)).Copy
代替您在原始代码中抛出错误行.Range(.Range("D3").End(xlToRight)).Copy
。
工作方式:
.Cells(.Rows.Count, "D").End(xlUp)
查找D列中最后使用的行.Cells(3, .Columns.Count).End(xlToLeft)
查找第3行中最后使用的列 .Range
构成了这两个单元格的范围。
相应地编辑评论:
要仅复制D列中的行,请使用…
.Range("D3", .Cells(.Rows.Count, "D").End(xlUp)).Copy
答案 1 :(得分:-1)
应用程序或对象定义的错误。通常表示某处有错字。
第二个with陈述有一个要点。
With .Sheets("Dump Lease & RMP Charges")
应该不是
With Sheets("Dump Lease & RMP Charges")
希望这可以消除应用程序/对象错误。