Private Sub CommandButton1_Click()
Dim Rng As Range, Dn As Range, n As Long, k As Variant
Set Rng = Range("A1", Range("A" & Rows.Count).End(xlUp))
With CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
For Each Dn In Rng
If Not .Exists(Dn.Value) Then
'Unique values in column"A" added to dictionary as "Key",
'with column "B" Range Object added as "Item"
'(Key) (item)
.Add Dn.Value, Dn.Offset(, 1)
Else
'Where Duplicate values of column "A" uniques are found
' The Column "B" range Object is added to the "Items" of Column "A" value
' using the "Union" function
Set .Item(Dn.Value) = Union(.Item(Dn.Value), Dn.Offset(, 1))
End If
Next
'As a result of the dictioary code above, each unique value in
'column "A" has its dictionary item set to the range of cells in column "B"
'that reflect that unique column "A" "Key"
'Loop through each "Key" in dictionary
For Each k In .keys
'Nb:- .item(k)is a range of cells, could be one , could be multi cells
If .Item(k).Count = 1 Then
'If the Number of cells in the "item) of the "Key" "K" is one,
'Then the "Item" of the "key" "K" is set in column "C" of the Row of ".item(k)"
.Item(k).Offset(, 1).Value = .Item(k)
Else
'If the Number of cells in the "item) of the "Key" "K" is Greater that one,
'Then the "Items" of the "key" "K" (a multi range)is conatenated togeter using the "Join" function.
'That value is then placed in the last row (column "C") of the range of cells in the range ".item(k)"
'NB:- .item(k).count, is the last row of the range ".item(k0"
.Item(k)(.Item(k).Count).Offset(, 1) = _
Join(Application.Transpose(.Item(k).Value), ",")
End If
Next k
End With
End Sub