组合唯一单元格的值
Combining unique cell's values
我正在尝试从 A 列中包含重复名称的列表中获取 B 列的组合值。
Sally Cookies, Apples
Jamie Pie
Sally Muffins
Bob Jam
Bob Pie
结果为:
Sally Cookies, Apples, Muffins
Jamie Pie
Bob Jam, Pie
我试过使用 Unique 排序,但我只得到名称的第一个实例
数据在 A 和 B 列中,例如:
运行 这个宏:
Sub GatherData()
Dim Na As Long, Nc As Long, v As String
Range("A:A").Copy Range("C1")
Range("C:C").RemoveDuplicates Columns:=1, Header:=xlNo
Na = Cells(Rows.Count, "A").End(xlUp).Row
Nc = Cells(Rows.Count, "C").End(xlUp).Row
For i = 1 To Nc
v = Cells(i, "C").Value
Cells(i, "D").Value = ""
For j = 1 To Na
If v = Cells(j, "A").Value Then
Cells(i, "D").Value = Cells(i, "D").Value & "," & Cells(j, "B").Value
End If
Next j
Next i
For i = 1 To Nc
Cells(i, "D").Value = Mid(Cells(i, "D").Value, 2)
Next i
End Sub
将产生:
我正在尝试从 A 列中包含重复名称的列表中获取 B 列的组合值。
Sally Cookies, Apples
Jamie Pie
Sally Muffins
Bob Jam
Bob Pie
结果为:
Sally Cookies, Apples, Muffins
Jamie Pie
Bob Jam, Pie
我试过使用 Unique 排序,但我只得到名称的第一个实例
数据在 A 和 B 列中,例如:
运行 这个宏:
Sub GatherData()
Dim Na As Long, Nc As Long, v As String
Range("A:A").Copy Range("C1")
Range("C:C").RemoveDuplicates Columns:=1, Header:=xlNo
Na = Cells(Rows.Count, "A").End(xlUp).Row
Nc = Cells(Rows.Count, "C").End(xlUp).Row
For i = 1 To Nc
v = Cells(i, "C").Value
Cells(i, "D").Value = ""
For j = 1 To Na
If v = Cells(j, "A").Value Then
Cells(i, "D").Value = Cells(i, "D").Value & "," & Cells(j, "B").Value
End If
Next j
Next i
For i = 1 To Nc
Cells(i, "D").Value = Mid(Cells(i, "D").Value, 2)
Next i
End Sub
将产生: