Excel 2007 将多行转置/合并为一

jzd*_*jzd 6 microsoft-excel-2007 microsoft-excel

我有这样的数据:

1001NCCN    3618127
1001NCCN    208478
1001NCCN    207316
1001TEMN    409889
1001TEMN    801651
1001TEMN    273134
1001TEMN    208478
1001TEMN    207316
Run Code Online (Sandbox Code Playgroud)

我需要转置/组合第一列中具有匹配值的行,最终结果如下:

1001NCCN    3618127 208478  207316      
1001TEMN    409889  801651  273134  208478  207316
Run Code Online (Sandbox Code Playgroud)

我查看了数据透视表和过滤器,但它们似乎都不能满足我的需求。有没有办法在 Excel 中做到这一点?

chr*_*sen 6

这是一个 VBA 解决方案

它依赖于分组的行,因此如果需要,请先排序

只需选择范围内的第一个单元格并运行...

Sub Mergeitems()
    Dim cl As Range
    Dim rw As Range

    Set rw = ActiveCell

    Do While rw <> ""
        ' for each row in data set
        '   find first empty cell on row
        Set cl = rw.Offset(0, 1)
        Do While cl <> ""
            Set cl = cl.Offset(0, 1)
        Loop

        ' if next row needs to be processed...
        Do While rw = rw.Offset(1, 0)
            cl = rw.Offset(1, 1)       ' move the data
            Set cl = cl.Offset(0, 1)   ' update pointer to next blank cell
            rw.Offset(1, 0).EntireRow.Delete xlShiftUp   ' delete old data
        Loop

        ' next row
        Set rw = rw.Offset(1, 0)
    Loop
End Sub
Run Code Online (Sandbox Code Playgroud)