如何在整个Excel列中打印一维数组而不循环?

时间:2019-04-16 10:51:42

标签: arrays excel vba printing range

我有一个大的一维数组,其中包含一些我想一次性在整个excel列中打印的数字。

这是我的代码:

Sub writeTimeDiff()
    Dim i As Long
    Dim j As Long
    Dim lastRow As Long
    Dim diffArr() As Variant

    lastRow = Sheets("AUX").Range("A1").End(xlDown).Row

    ReDim diffArr(lastRow)

    j = 2

    For i = 0 To lastRow
        'Store the difference between two values in a row in the array
        tsIN = Sheets("AUX").Cells(j, 3).value
        tsOUT = Sheets("AUX").Cells(j, 4).value
        diffArr(i) = tsOUT - tsIN
        j = j + 1
    Next i

    'I'd like it to print the array throughout the entire F column (Starting in F2) 
    Sheets("AUX").Range("F2:F" & lastRow).value = diffArr
End Sub

但似乎只打印数组的第一个值:

1

这是我真正需要的:

2

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:2)

默认情况下,一维数组被视为行,因此您需要对其进行转置以转换为列。

Sheets("AUX").Range("F2:F" & lastRow).Value = Application.Transpose(diffArr)