在excel中合并两行,并在最后一列旁边移动一列

时间:2018-08-24 10:15:17

标签: excel

我如何在excel中通过将其余各列的内容相加,将在A列中共享相同值的所有行合并到追加到第一行的最后一个现有列的新列中?

A B C
A 2 3
A 4 6

Result:
A B C 2 3 4 6

2 个答案:

答案 0 :(得分:0)

您可以使用Power Query:

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Change All to Text" = Table.TransformColumnTypes(Source, List.Transform(Table.ColumnNames(Source), each {_, type text})),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Change All to Text", {"Column1"}, "Temp1", "Value"),
    #"Removed Temp1" = Table.RemoveColumns(#"Unpivoted Other Columns",{"Temp1"}),
    #"Grouped Column1" = Table.Group(#"Removed Temp1", {"Column1"}, {{"Temp2", each _, type table}}),
    #"Expanded Values" = Table.AddColumn(#"Grouped Column1", "Value", each Text.Combine([Temp2][Value],",")),
    #"Removed Temp2" = Table.RemoveColumns(#"Expanded Values",{"Temp2"}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Removed Temp2", "Value", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv))
in
    #"Split Column by Delimiter"

答案 1 :(得分:0)

我认为一点VBA也可以解决问题。像这样:

Sub MergeAppend()

  Dim row As Range
  Dim lastVal As String
  Dim lastRow, lastCol, i As Integer

  For Each row In ActiveSheet.UsedRange.Rows
    If lastVal = row.Cells(1, 1).Value2 Then
      For i = 2 To row.SpecialCells(xlCellTypeLastCell).Column
        If Cells(row.row, i).Value2 <> "" Then
          Cells(lastRow, lastCol).Value2 = Cells(row.row, i).Value2
          lastCol = lastCol + 1
        End If
      Next i
    Else
      lastVal = row.Cells(1, 1).Value2
      lastRow = row.row
      lastCol = row.SpecialCells(xlCellTypeLastCell).Column + 1
    End If


  Next row

End Sub