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

问题描述 投票:-1回答:2

如何在Excel中合并在A列中共享相同值的所有行,方法是将每个其余列的内容添加到追加到第一行的最后一个现有列的新列中?

A B C
A 2 3
A 4 6

Result:
A B C 2 3 4 6
excel
2个回答
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"

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
© www.soinside.com 2019 - 2024. All rights reserved.