1
我一直在试图将一张简单的表从一张表复制到另一张表的最后一行。 最初我尝试使用数组,因为两个表都有不同的结构(列不在同一顺序,所以我不能只复制粘贴&),但我总是得到错误1004. 现在我放弃了,并改变了所以他们两个表都有相同的结构,现在我可以简单地复制&粘贴,但我仍然得到相同的错误。 这是我到目前为止。我知道这是一件很简单的事情,但我不知道我错在哪里。如何将数组粘贴到范围[VBA] - 错误1004
Sub testy()
Dim rowsIn, rowsOut As Long
With Worksheets("Sheet1")
rowsIn = .Cells.SpecialCells(xlLastCell).Row
.Range(.Cells(4, 1), .Cells(rowsIn, 3)).Copy
End With
With Worksheets("Sheet2")
rowsOut = .Cells.SpecialCells(xlLastCell).Row
.Range(.Cells(rowsOut + 1, 3)).PasteSpecial xlPasteValues
End With
End Sub
编辑:解决按添Williams的建议。但是,我仍然很好奇,如何以数组方式完成这项工作,就像我最初的意图一样。 假设Sheet1中的数据具有与Sheet2不同的顺序的列,我尝试使用临时数组来订购列,以便我可以将其粘贴。我设法填充数组很好,但无法弄清楚如何将数组的内容导入到Sheet2中。添加了我用来填充数组的代码(以非常不利的方式)。
Sub testy2ElectricBoogaloo()
Dim arr() As Variant
Dim rowsIn, rowsOut, i As Long
With Worksheets("Sheet1")
rowsIn = .Cells.SpecialCells(xlLastCell).Row
ReDim arr(1 To rowsIn - 3, 1 To 5)
'Array populated with a loop because columns are not in the same order, don't know if this is the most efficient method
For i = 1 To UBound(arr)
arr(i, 1) = "Constant1" 'data collected from other source
arr(i, 2) = "Constant2" 'data collected from other source
arr(i, 3) = .Cells(i + 3, 2).Value
arr(i, 4) = .Cells(i + 3, 1).Value
arr(i, 5) = .Cells(i + 3, 3).Value
Next i
End With
End Sub
谢谢,这绝对是我需要的。感谢您的快速回复 - 我现在将继续卸载Office,因为它不知道如此基本的内容。 –
不是真的那么基本;-) –
对不起,让询问在一个封闭的Q,但我会怎么做,如果我想检索一个数组,而不是一个范围内的数据(如我本来想这样做,但放弃了)? –