This will handle your entire data set. Have a look at the comments and update your ranges in the two places it specifies. To be honest though where's your data pulled from? I'm assuming a database. You should probably handle this in your data feed instead
Public Sub ValuestoStringSeparated()
Dim Data As Variant, Results As Variant, tmp As Variant
Dim Dict As Object
Dim i As Long
Dim Key
Set Dict = CreateObject("Scripting.Dictionary")
' Update this to your sheet Ref
With ActiveSheet
Data = .Range(.Cells(2, 1), .Cells(.Cells(.Rows.Count, 1).End(xlUp).Row, 2)).Value2
End With
' Add your raw data to Dictionary
For i = LBound(Data, 1) To UBound(Data, 1)
If Not Dict.Exists(Data(i, 1)) Then
ReDim tmp(0)
tmp(0) = Data(i, 2)
Dict.Add Key:=Data(i, 1), Item:=tmp
Else
tmp = Dict(Data(i, 1))
ReDim Preserve tmp(LBound(tmp) To UBound(tmp) + 1)
tmp(UBound(tmp)) = Data(i, 2)
Dict(Data(i, 1)) = tmp
End If
Erase tmp
Next i
' Print your Data to sheet
ReDim Results(1 To Dict.Count, 1 To 2)
i = 0
For Each Key In Dict.keys
i = i + 1
Results(i, 1) = Key
Results(i, 2) = Join(Dict(Key), ", ")
Next Key
' Update with your desired output destination
With ActiveSheet.Range("D2")
.Resize(UBound(Results, 1), UBound(Results, 2)).Value2 = Results
End With
End Sub