多行到单行操作

我有一堆Excel中的数据,看起来像这样:

Address Bins address1 94 Gallon address1 60 Gallon address1 94 Gallon address1 35 Gallon address1 94 Gallon address1 35 Gallon address1 60 Gallon address1 94 Gallon我想排除具有相同地址的行,并将它们合并成一行。

Address Bin1 Bin2 Bin3 Bin4 Bin5 Bin6 Bin7 Bin8 address1 94 Gallon 60 Gallon 94 Gallon 35 Gallon 94 Gallon 35 Gallon 60 Gallon 94 Gallon

我将如何做到这一点?

要使用公式列出单个行中每个地址的所有容器,需要:

  1. 在名为Key的列A的示例表中添加一列(参见图1)

在这里输入图像说明 图。1

:用于检索每个地址的相应分

A2input该公式,然后复制到最后一个logging

 =CONCATENATE($B2,".",COUNTIF($B$1:$B2,$B2)) 
  1. E1开始添加一个范围来列出地址\ bin表(见图2)

在这里输入图像说明

图2

地址 :从列B提取唯一值

E2input此Formula Array ,然后复制到最后一条logging

 {=IFERROR(INDEX($B$2:$B$22, MATCH(SUM(COUNTIF(E$1:E1,$B$2:$B$22)), COUNTIF($B$2:$B$22,"<"&$B$2:$B$22),0)),"")} 

计数 :每个地址的垃圾箱数。 用这个来扩大表格的宽度

F2input该公式,然后复制到最后一条logging

 =IF(EXACT($E2,""),"",COUNTIF($B:$B,$E2)) 

Bin.1到Bin.n :垃圾桶的内容

G2input该公式,然后复制到所有箱的最后一个logging

 =IFERROR(VLOOKUP(CONCATENATE($E2,".",RIGHT(G$1)),$A:$C,3,0),"") 

突出显示行。 复制。 当你粘贴,右键点击,所以看到选项。 去转换数据的那个。 它将全部粘贴到列而不是一行。

下面的代码应该把你的地址和结合起来。 之后,你只需要添加你的列标题

 Sub ConsolidateRows_MultipleCells() 'takes rows and consolidate one or many cells, based on one or many cells matching with above or below rows. Dim lastRow As Long, i As Long, j As Long Dim colMatch As Variant, colConcat As Variant, lColDest As Long '**********PARAMETERS TO UPDATE**************** Const strMatch As String = "A" 'columns that need to match for consolidation, separated by commas Const strConcat As String = "B" 'columns that need consolidating, separated by commas Const lDest As Long = 2 'starting column for the consolidated items '*************END PARAMETERS******************* application.ScreenUpdating = False 'disable ScreenUpdating to avoid screen flashes colMatch = Split(strMatch, ",") colConcat = Split(strConcat, ",") Cells(1, 1).CurrentRegion.Sort key1:=Cells(1, colMatch(0)), order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _ False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2 _ :=xlSortNormal lastRow = range("A" & Rows.Count).End(xlUp).Row 'get last row lColDest = lDest For i = lastRow To 2 Step -1 'loop from last Row to one For j = 0 To UBound(colMatch) If Cells(i, colMatch(j)) <> Cells(i - 1, colMatch(j)) Then lColDest = lDest GoTo nxti End If Next For j = 0 To UBound(colConcat) range(Cells(i, strConcat), Cells(i, 1).End(xlToRight)).Copy Cells(i - 1, 1).End(xlToRight).Offset(, 1) lColDest = lColDest + 1 Next Rows(i).Delete nxti: Next application.ScreenUpdating = True 'reenable ScreenUpdating End Sub