Excel VBA宏代码,用于在单个列中粘贴列数据

时间:2018-06-16 07:27:47

标签: vba excel-vba excel

pasting multiple columns under single

有多个列超过3

1 个答案:

答案 0 :(得分:1)

使用2个循环并逐行循环然后将每个值存储到1D数组中,该数组可以一次性写入目标列。在循环期间,一次性分配比写入工作表更快。

Option Explicit
Public Sub ReArrange()
    Dim iRow As Long, iCol As Long, arr(), arr2(), counter As Long
    With ActiveSheet
        arr = .Range("C2:E4").Value '<== Assume data is in this range
        ReDim arr2(1 To UBound(arr, 1) * UBound(arr, 2))
        For iRow = LBound(arr, 1) To UBound(arr, 1)
            For iCol = LBound(arr, 2) To UBound(arr, 2)
                counter = counter + 1
                arr2(counter) = arr(iRow, iCol)
            Next
        Next
        .Range("B1").Resize(UBound(arr2), 1).Value = Application.WorksheetFunction.Transpose(arr2)
    End With
End Sub