Основываясь на приведенном примере что-то, как это должно работать (я говорю РЕКОМЕНДУЕМЫМ, потому что у меня нет Excel под рукой, так что код не тестировался):
Sub JoinByID()
Dim dct As Dictionary
Dim i As Long, j As Long
Dim iIDCol As Integer
Dim rFirst As Range
Dim sID As String
Dim col As Collection
Dim ws As Worksheet, wsNew As Worksheet
Dim vLoopIDs As Variant, vLoopVals As Variant
Set rFirst = Sheets(1).Range("A1") 'change to where your data starts
Set ws = rFirst.Parent
iIDCol = rFirst.Column
' loop through data and collect based on ID
For i = rFirst.Row + 1 To rFirst.End(xlDown).Row
sID = ws.Cells(i, iIDCol).Value
If dct.Exists(sID) Then
dct(sID).Add ws.Cells(i, iIDCol + 1).Value
Else
Set col = New Collection
col.Add ws.Cells(i, iIDCol + 1).Value
dct.Add sID, col
End If
Next i
' drop the data in a new sheet
Set wsNew = Worksheets.Add
i = 0
With wsNew
For Each vLoopIDs In dct.Keys
i = i + 1
.Cells(i, 1) = vLoop
j = 1
For Each vLoopVals In dct(vLoop)
j = j + 1
.Cells(i, j) = vLoopVals
Next vLoopVals
Next vLoopIDs
End With
End Sub
Примечание: вам нужно добавить ссылку на Runtime Microsoft Scripting Runtime, чтобы иметь Dictionary
.
Да - вы можете использовать VBA для этого. –