使用 excel 显示重复 ID 的所有值
Displaying all the values of the repeating id using excel
我左边有两列A列和B列COlumn A有repating ids,columb b有相应的值。我如何过滤 A 值列并在右侧显示它们的对应值。
A 列 B 列 1000 5 1000 ann
2000 克里斯 2000 56 2001 贝丝 3000 马克 3000 1
输出应显示:A 列,B 列
1000 5, ann 2000 56, beth 3000 马克, 1
将此粘贴到 C 列相关行并复制到剩余单元格。
=IF(A2=A1,C1&","&B2,B2)
这取决于您的具体场景,但如果您的 A 值是升序排列,您可以这样做:
首先,为连接添加以下宏:
Function ConCat(Delimiter As Variant, ParamArray CellRanges() As Variant) As String
Dim Cell As Range, Area As Variant
If IsMissing(Delimiter) Then Delimiter = ""
For Each Area In CellRanges
If TypeName(Area) = "Range" Then
For Each Cell In Area
If Len(Cell.Value) Then ConCat = ConCat & Delimiter & Cell.Value
Next
Else
ConCat = ConCat & Delimiter & Area
End If
Next
ConCat = Mid(ConCat, Len(Delimiter) + 1)
End Function
然后,将以下公式放入结果列中:
=ConCat(",",INDIRECT("B"&MATCH(D3,$A:$A,0)&":B"&MATCH(D3,$A:$A,1)))
这基本上是找到搜索值的第一个和最后一个匹配项,然后连接该范围内的所有值。
请注意,这是专门针对这些单元格中的日期的:
但是,只要将起始数据行 #(使用 ROW() 函数)添加到两个匹配结果中,就可以轻松修改数据所在的任何位置。
使用 VBA 自动过滤器的示例。配置列 A、B、O(输出)、开始行和结束行的位置以适合。
Option Explicit
Sub xfrMatches()
Dim ws As Worksheet
Dim colA As Long, colB As Long, strowA, endRowA As Long
Dim colO As Long, stRowO As Long, endRowO As Long
Dim c As Long
Dim crit1 As Variant
Dim tmpStr As String
Dim cl As Range
Set ws = Sheets("Sheet1")
colA = 1
colB = 2
strowA = 2
colO = 7
stRowO = 3
ws.AutoFilterMode = False
With ws
endRowA = .Cells(Rows.Count, colA).End(xlUp).Row
endRowO = .Cells(Rows.Count, colO).End(xlUp).Row
For c = stRowO To endRowO
ws.AutoFilterMode = False
crit1 = .Cells(c, colO).Value
With .Range(.Cells(strowA, colA), .Cells(endRowA, colB))
.AutoFilter
.AutoFilter Field:=1, Criteria1:=crit1
End With
With .AutoFilter.Range
tmpStr = ""
For Each cl In .Columns(colB).Offset(1, 0).SpecialCells(xlCellTypeVisible)
tmpStr = tmpStr & "," & cl.Value
Next cl
ws.AutoFilterMode = False
End With
.Cells(c, colO).Offset(0, 1).Value = Mid(tmpStr, 2, Len(tmpStr) - 2)
Next c
End With
End Sub
A 列 B 列 1000 5 1000 ann 2000 克里斯 2000 56 2001 贝丝 3000 马克 3000 1
输出应显示:A 列,B 列
1000 5, ann 2000 56, beth 3000 马克, 1
将此粘贴到 C 列相关行并复制到剩余单元格。
=IF(A2=A1,C1&","&B2,B2)
这取决于您的具体场景,但如果您的 A 值是升序排列,您可以这样做:
首先,为连接添加以下宏:
Function ConCat(Delimiter As Variant, ParamArray CellRanges() As Variant) As String
Dim Cell As Range, Area As Variant
If IsMissing(Delimiter) Then Delimiter = ""
For Each Area In CellRanges
If TypeName(Area) = "Range" Then
For Each Cell In Area
If Len(Cell.Value) Then ConCat = ConCat & Delimiter & Cell.Value
Next
Else
ConCat = ConCat & Delimiter & Area
End If
Next
ConCat = Mid(ConCat, Len(Delimiter) + 1)
End Function
然后,将以下公式放入结果列中:
=ConCat(",",INDIRECT("B"&MATCH(D3,$A:$A,0)&":B"&MATCH(D3,$A:$A,1)))
这基本上是找到搜索值的第一个和最后一个匹配项,然后连接该范围内的所有值。
请注意,这是专门针对这些单元格中的日期的:
但是,只要将起始数据行 #(使用 ROW() 函数)添加到两个匹配结果中,就可以轻松修改数据所在的任何位置。
使用 VBA 自动过滤器的示例。配置列 A、B、O(输出)、开始行和结束行的位置以适合。
Option Explicit
Sub xfrMatches()
Dim ws As Worksheet
Dim colA As Long, colB As Long, strowA, endRowA As Long
Dim colO As Long, stRowO As Long, endRowO As Long
Dim c As Long
Dim crit1 As Variant
Dim tmpStr As String
Dim cl As Range
Set ws = Sheets("Sheet1")
colA = 1
colB = 2
strowA = 2
colO = 7
stRowO = 3
ws.AutoFilterMode = False
With ws
endRowA = .Cells(Rows.Count, colA).End(xlUp).Row
endRowO = .Cells(Rows.Count, colO).End(xlUp).Row
For c = stRowO To endRowO
ws.AutoFilterMode = False
crit1 = .Cells(c, colO).Value
With .Range(.Cells(strowA, colA), .Cells(endRowA, colB))
.AutoFilter
.AutoFilter Field:=1, Criteria1:=crit1
End With
With .AutoFilter.Range
tmpStr = ""
For Each cl In .Columns(colB).Offset(1, 0).SpecialCells(xlCellTypeVisible)
tmpStr = tmpStr & "," & cl.Value
Next cl
ws.AutoFilterMode = False
End With
.Cells(c, colO).Offset(0, 1).Value = Mid(tmpStr, 2, Len(tmpStr) - 2)
Next c
End With
End Sub