从Array到Array到Range

时间:2015-12-17 13:29:11

标签: arrays excel vba excel-vba

我创建了一个TempTable作为ReDim TempTable(1 To RowSize, 1 To 3),对于每个单元格,我有一个55值的向量。请参阅下面的图片以更好地理解

enter image description here

现在,我想在工作表中复制TempTable的全部内容。范围应该是[7576x55,3],因为TempTable有7576行。有没有聪明而快速的方法呢?

1 个答案:

答案 0 :(得分:1)

这应该顺利进行(如果每列中的向量具有相同的大小):

Sub test_gmeroni()
Dim TempTable() As Variant, _
    wS As Worksheet, _
    Rg As Range, _
    InBound As Long, _
    i As Long, _
    j As Long

Set wS = ThisWorkbook.Sheets("OutPut")
Set Rg = wS.Range("A1")
ReDim TempTable(1 To 7576, 1 To 3)

For i = LBound(TempTable, 1) To UBound(TempTable, 1)
    For j = LBound(TempTable, 2) To UBound(TempTable, 2)
        'Calculate vectors size
        InBound = UBound(TempTable(i, j), 1)
        'Put vector on sheet
        Rg.Offset(0, j - 1).Resize(InBound, 1).Value2 = TempTable(i, j)
    Next j
    'Select next cell to start printing next row
    Set Rg = Rg.Offset(InBound + 1, 0)
Next i

End Sub