Here is a somewhat different approach. We create a User Defined Object (Class) which consists of each unique Column A item, and a collection of the associate Column B items.
We create a Collection of these class objects, using the property of the Collection object that two items cannot have the same key. If they do, it creates a trappable error, which we can then use to add the colB item to the ColB collection in that class.
An advantage is understandable properties, and easy maintainablility. Also, by doing all the work in VBA and using VBA arrays, speed is quite good, even with large databases.
I named the Class module cColaStuff
, and you will have to rename it as such when you insert it. But you can name it anything.
Class Module
'RENAME this module **cCOLaStuff**
Option Explicit
Private pColA As String
Private pColB As String
Private pColBs As Collection
Public Property Get ColA() As String
ColA = pColA
End Property
Public Property Let ColA(Value As String)
pColA = Value
End Property
Public Property Get ColB() As String
ColB = pColB
End Property
Public Property Let ColB(Value As String)
pColB = Value
End Property
Public Property Get ColBs() As Collection
Set ColBs = pColBs
End Property
Public Function ADDColB(Value As String)
pColBs.Add Value
End Function
Private Sub Class_Initialize()
Set pColBs = New Collection
End Sub
Regular Module
Option Explicit
Sub CombineAB()
Dim cC As cCOLaStuff, colC As Collection
Dim wsSrc As Worksheet, wsResults As Worksheet, rResults As Range
Dim vSrc As Variant, vResults As Variant
Dim I As Long, J As Long
'Change sheets as needed
Set wsSrc = Worksheets("sheet1")
Set wsResults = Worksheets("sheet2")
Set rResults = wsResults.Cells(1, 1)
'Get the source data
With wsSrc
vSrc = .Range(.Cells(1, 1), .Cells(.Rows.Count, 2).End(xlUp))
End With
'Collect the data, ColA as the key, and a collection of ColB stuff
Set colC = New Collection
On Error Resume Next 'to detect the duplicates
For I = 2 To UBound(vSrc, 1) 'skip the header row
Set cC = New cCOLaStuff
With cC
.ColA = vSrc(I, 1)
.ColB = vSrc(I, 2)
.ADDColB .ColB
colC.Add Item:=cC, Key:=CStr(.ColA)
Select Case Err.Number
Case 457 'we have a duplicate, so add ColB to previous object
Err.Clear
colC(CStr(.ColA)).ADDColB .ColB
Case Is <> 0 'debug stop
Debug.Print Err.Number, Err.Description
Stop
End Select
End With
Next I
On Error GoTo 0
'create the results array
'row count = ColBs with the highest count (+1 for the header row)
J = 0
For I = 1 To colC.Count
J = IIf(J >= colC(I).ColBs.Count, J, colC(I).ColBs.Count)
Next I
'Column count = number of collection items
ReDim vResults(0 To J, 1 To colC.Count)
'Populate the array
For J = 1 To UBound(vResults, 2)
I = 0
With colC(J)
vResults(I, J) = .ColA
For I = 1 To .ColBs.Count
vResults(I, J) = .ColBs(I)
Next I
End With
Next J
'write the results to the worksheet
Set rResults = rResults.Resize(UBound(vResults, 1) + 1, UBound(vResults, 2))
With rResults
.EntireColumn.Clear
.Value = vResults
With .Rows(1)
.Font.Bold = True
.HorizontalAlignment = xlCenter
End With
End With
End Sub