从一个长列转换为多个列

时间:2011-05-26 17:29:56

标签: excel vba

我需要一点帮助,或者一个可以转换大数据集(960000行)的VBA脚本,格式如下所示。所有数据都在一栏

TRIP_ID | OBJECTID | CPR_VEJNAV | ADM_VEJSTA | ADM_VEJKLA | vejid | vejkl | Shape_Length
2626    |  value   |  value     | value      | value      | value | value | value
..
..
2626    | value   |  value     | value      | value      | value | value | value
64646   | value   |  value     | value      | value      | value | value | value
..
..
..
64646   | value   |  value     | value      | value      | value | value | value

我想将数据转换为多个列,每个TRIP_ID一列,如下所示:

TRIP_ID | .....     | TRIP_ID .....  | And so on
2626    | .....     | 64646   ..... 
..
..
2626    | ......    |  64646      .....

依此类推,我有大约1800 TRIP_ID 简而言之: 从一个长列转换为基于TRIP_ID

的多个列

1 个答案:

答案 0 :(得分:1)

在运行其他人的代码之前,请务必备份数据

Sub SplitToColumns()

    Dim rCell As Range
    Dim sCurrent As String
    Dim rLast As Range
    Dim lRowStart As Long

    Application.EnableEvents = False

    Set rLast = Sheet1.Range("A2").End(xlDown).Offset(1, 0)
    rLast.Value = "End"

    For Each rCell In Sheet1.Range("A2", rLast).Cells
        If Split(rCell.Value, "|")(0) <> sCurrent Then
            If lRowStart > 1 Then
                rCell.Offset(lRowStart - rCell.Row, 0).Resize(rCell.Row - lRowStart, 1).Copy
                Sheet1.Cells(2, Sheet1.Columns.Count).End(xlToLeft).Offset(0, 1).Resize(rCell.Row - lRowStart, 1).PasteSpecial xlValues
            End If
            lRowStart = rCell.Row
            sCurrent = Split(rCell.Value, "|")(0)
        End If
    Next rCell

    rLast.ClearContents
    Application.CutCopyMode = False
    Application.EnableEvents = True

End Sub