我有这段代码将填充一个数组
Sub rangearray()
Dim arr() As Variant
Dim Rng As Range
Dim myCell As Range
Dim i As Integer
Set Rng = ActiveSheet.Range("G10:G14")
For Each myCell In Rng
ReDim Preserve arr(i)
arr(i) = myCell
i = i + 1
Next myCell
ActiveSheet.Range("H10:H14") = arr()
End Sub
在这里你可以看到监视窗口中的值是已经加载的
除了,当我将数组添加回工作簿时,它只会粘贴回数组的第一个元素。
是否可以将整个数组粘贴到工作表而不必遍历数组?
在查看了 Sorceri 的链接后,我修改了代码以使用 .Transpose 函数,所以我修改后的代码现在看起来像这样:
Sub rangearray()
Dim arr() As Variant
Dim Rng As Range
Dim myCell As Range
Dim i As Integer
Set Rng = ActiveSheet.Range("A1:A5")
For Each myCell In Rng
ReDim Preserve arr(i)
arr(i) = myCell
i = i + 1
Next myCell
ActiveSheet.Range("B1:B5") = WorksheetFunction.Transpose(arr)
End Sub