VBA Excel: Paste large array to range

最后都变了- 提交于 2020-01-17 01:22:28

问题


I'm working in VBA for excel. I have an array called "aKey" (1 by 137,000 strings but exact size is subject to change so making code generic is a neccesity). I need to paste aKey to the first column of a a worksheet. So far i have tried Range(.Offset(1,0),.Offset(UBound(aKey)+1,0)).Value = aKey but this seems to only paste 137,000 versions of the first entry of the array. I have also tried Range(.Offset(1,0),.Offset(UBound(aKey)+1,0)).Value = WorksheetFunction.Transpose(aKey) which also didn't work. Through a google search I did find that the Transpose function has a limited pasting size which may very well be the problem there. Does anybody know of a method to avhieve my goal? Thanks


回答1:


Sub Tester()

    Dim a1(), a2(), i As Long, ub As Long

    ReDim a1(1 To 1, 1 To 137000)
    'load source array ("wrong" shape)
    For i = 1 To 137000
        a1(1, i) = i
    Next i

    ub = UBound(a1, 2)

    ReDim a2(1 To ub, 1 To 1) 'resize a2 ("right" shape) to match a1

    ' "flip" the a1 array into a2 
    For i = 1 To ub
        a2(i, 1) = a1(1, i)
    Next i

    'drop a2 to worksheet
    ActiveSheet.Range("a1").Resize(ub, 1).Value = a2

End Sub



回答2:


Here is an example of creating an array for a single column and placing it in a column:

Sub qwerty()
Dim aKey(1 To 137000, 1 To 1) As Variant

For i = 1 To 137000
    aKey(i, 1) = Rnd
Next i

Range("A1:A137000") = aKey
End Sub


来源:https://stackoverflow.com/questions/25019868/vba-excel-paste-large-array-to-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!