Excel VBA - 水平转置数组

Excel VBA - Transpose an Array Horizontally

我有一个数组 (dict1),我想使用以下代码从单元格 T1-Z1 粘贴

With Sheets("Raw_Data")
  .Range(.Cells(1, 20), .Cells(1, 26)).Resize(dict1.Count).Value = Application.Transpose(dict1.keys)
End With

然而,当我使用它时,它将值从 T1-T7 粘贴到数组中,然后从 T-Z 列复制它。无论如何我可以改变它所以它转置阵列水平移动

而不是

.Range(.Cells(1, 20), .Cells(1, 26)).Resize(dict1.Count).Value = Application.Transpose(dict1.keys)

使用

.Range("T1").Resize(1, UBound(Application.Transpose(dict1.keys))) = dict1.keys

尝试以下代码

Sub Demo()
    Dim dict1 As Object
    Set dict1 = CreateObject("Scripting.Dictionary")

    'assume following are the disctionary items
    dict1.Add "Aaa", 1
    dict1.Add "Bbb", 2
    dict1.Add "Ccc", 3
    dict1.Add "Ddd", 4
    dict1.Add "Eee", 5
    dict1.Add "Fff", 6
    dict1.Add "Ggg", 7

    'output dictionary horizontally
    With Sheets("Raw_Data")
        .Range("T1").Resize(1, UBound(Application.Transpose(dict1.Keys))) = dict1.Keys
    End With
End Sub

结果如下图。

原始数组是从垂直范围加载的

avarPercentValues() = .Range(cstrColTotals & clngAverageSpread & ":" & cstrColTotals & clngValidGain).Value2

最简单快捷的横向保存方式:

.Range(cstrColTAvgSpread & lngDataRow & ":" & cstrColTValidGain & lngDataRow).Value2 = Application.Transpose(avarPercentValues())

cxxxYY 是列的常量 在我的机器上,我失去了 4 µs om 转置。