错误代码1004复制/粘贴VBA无法解决

我一直在看这5个多小时,不能find一个正确的解决scheme。 这不是我主要的工作,而是我在工作中帮助的。

基本上我是从一张纸上复制过来的行到另一张纸,并把它放在A列的最后一行来粘贴。

这工作完美find之前,我做了一些改变,现在它完全破碎,任何帮助感激地赞赏,这里是破碎的意大利面条代码….

Sheets("Working Sheet").Select Selection.Copy Sheets("Sent Items").Select Dim LastRow As Long With ActiveSheet LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1 End With Range("A" & LastRow).Select ActiveSheet.Paste Application.CutCopyMode = False Columns("K:K").EntireColumn.AutoFit Sheets("Sent Items").Select 

它会导致错误1004,说大小需要是相同的? 粘贴导致错误。 任何帮助是好的一直在寻找答案。

你可以重构你的代码,如下所示:

 Worksheets("Working Sheet").Select Selection.SpecialCells(xlCellTypeVisible).Copy Destination:=Worksheets("Sent Items").Cells(Rows.Count, 1).End(xlUp).Offset(1) 

或者,如果您只想粘贴值,

 Dim area As Range Worksheets("Working Sheet").Select For Each area In Selection.SpecialCells(xlCellTypeVisible).Areas Worksheets("Sent Items").Cells(Rows.Count, 1).End(xlUp).Offset(1).Resize(area.Rows.Count, area.Columns.Count).Value = area.Value Next area 

由于您正在复制已过滤的行,所以使用SpecialCells方法总是一个很好的做法。

请参阅下面的重构代码。 此外,总是最好避免使用select和直接与对象工作。

 Dim wsWorking as Worksheet Set wsWorking = Sheets("Working Sheet") With wsWorking .Select Selection.SpecialCells(xlCellTypeVisible).Copy End With Dim wsSent as Worksheet Set wsSent = Sheets("Sent Items") With wsSent Dim LastRow As Long LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1 .Range("A" & LastRow).PasteSpecial .Columns("K:K").EntireColumn.AutoFit .Range("A1").Select 'to make sure you end up on that sheet End With