在Excel中组合两行

时间:2016-04-19 19:03:46

标签: excel vba pivot

我在Excel表格中有两行。所有列都是相同的,除了一个。像 enter image description here

我怎样才能做到这一点?我对Pivot概念不太熟悉。

任何帮助?

问候。

1 个答案:

答案 0 :(得分:0)

希望这是你想要的:

Sub QuickCombine()
    Dim X()
    Dim Y()
    Dim objDic As Object
    Dim lngRow As Long
    Dim lngCol As Long
    Dim ws As Worksheet

    X = Range([a1], Cells(Rows.Count, "G").End(xlUp))
    Y = X
    Set objDic = CreateObject("scripting.dictionary")

    For lngRow = 1 To UBound(X, 1)
        If Not objDic.exists(LCase$(X(lngRow, 1) & X(lngRow, 2))) Then
            objDic.Add LCase$(X(lngRow, 1) & X(lngRow, 2)), lngRow
        Else
            Y(lngRow, 1) = vbNullString
            Y(objDic.Item(LCase$(X(lngRow, 1) & X(lngRow, 2))), 3) = Y(objDic.Item(LCase$(X(lngRow, 1) & X(lngRow, 2))), 3) & "," & X(lngRow, 3)
        End If
    Next

    Set ws = Sheets.Add
    [a1].Resize(UBound(X, 1), UBound(X, 2)) = Y
    Columns("A").SpecialCells(xlBlanks).EntireRow.Delete

End Sub

来自here