"Transposing" 一些带有 ID 字段的列在复制其他数据时变成行

"Transposing" some columns with ID fields into rows while copying the other data

假设我在 Excel 中继承了一个处理流程和个人的 table,它(在其他数千行中)看起来与此类似。

ID             | Name        | Quality1   | Quality2   | ... | QualityN   |
...
234,014,828,423  James         Low          Hot           .    Blue
212,552,211      Mark          Low          Cold          .    Red
845              Amy           High         Hot           .    White
...

我打算稍后在 Access 中使用此数据,作为参考 table 其中每个 ID 号都是不同的并且有数据与之一起使用。第一步显然是使用 Excel 中的 text to column 工具来分解 ID 类别。这给我们留下了看起来像这样的东西。

ID |   |   |   | Name        | Quality1   | Quality2   | ... | QualityN   |
... 
234 014 828 423  James         Low          Hot           .    Blue
212 552 211      Mark          Low          Cold          .    Red
845              Amy           High         Hot           .    White
...

然而,下一部分让我卡住了。需要什么过程(仅使用 Excel、Access 和关联的 VBA)才能得到我想要的结果?

ID             | Name        | Quality1   | Quality2   | ... | QualityN   |
... 
234              James         Low          Hot           .    Blue
014              James         Low          Hot           .    Blue
828              James         Low          Hot           .    Blue
423              James         Low          Hot           .    Blue
212              Mark          Low          Cold          .    Red
552              Mark          Low          Cold          .    Red
211              Mark          Low          Cold          .    Red
845              Amy           High         Hot           .    White
...

我的直觉告诉我使用范围从 0 到 9999 的数字 table 然后到 JOIN 在 Access 中逐位处理数据,但这是代码和时间密集型的,因为以及令人难以置信的野蛮和僵化。有没有更优雅的方法供我设计解决方案?

如果您想在访问 Access 之前用 Excel 执行此操作,那么一个简单的嵌套 for 循环如何获取数据并放入另一个 sheet?

这个小函数就可以做到这一点:

Option Explicit

Function Consolidate()

    Dim x As Long, y As Long, z As Long
    Dim OutputRow As Long, OutputCol As Long
    Dim MaxRow As Long, MaxIdCol As Long, MaxCol As Long
    Dim HomeSheet As String, NewSheet As String

    'Initialize
    MaxRow = Cells(Cells.Rows.Count, 1).End(xlUp).Row 'Last row with an ID
    MaxIdCol = Cells(1, 1).End(xlToRight).Column - 1 ' Last column with an ID
    MaxCol = Cells(1, Cells.Columns.Count).End(xlToLeft).Column 'Last column overall
    HomeSheet = ActiveSheet.Name 'Where we start from
    NewSheet = Sheets.Add.Name 'Place to put new stuff
    OutputRow = 0 'Counter for where we're putting data

    'Loop over each row
    For x = 1 To MaxRow
        'Loop over each ID column in that row
        For y = 1 To MaxIdCol
            'Is there an ID in this cell?
            If Sheets(HomeSheet).Cells(x, y) <> "" Then
                'Reset loop variables
                OutputRow = OutputRow + 1
                OutputCol = 1
                'Drop the ID in
                Sheets(NewSheet).Cells(OutputRow, OutputCol) = Sheets(HomeSheet).Cells(x, y)
                'copy over the other values
                For z = MaxIdCol + 1 To MaxCol
                    OutputCol = OutputCol + 1
                    Sheets(NewSheet).Cells(OutputRow, OutputCol) = Sheets(HomeSheet).Cells(x, z)
                Next z
            End If
        Next y
    Next x

End Function