1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| Option Explicit
Sub test()
Dim a, i As Long, j As Long, w(), n As Long, e
With Sheets("Sheet1").Cells(1).CurrentRegion
a = .Value
With CreateObject("Scripting.Dictionary")
.CompareMode = 1
For i = 2 To UBound(a, 1)
If Not .exists(a(i, 2)) Then
ReDim w(1 To UBound(a, 2), 1 To 1)
Else
w = .Item(a(i, 2))
ReDim Preserve w(1 To UBound(a, 2), 1 To UBound(w, 2) + 1)
End If
For j = 1 To UBound(a, 2)
w(j, UBound(w, 2)) = a(i, j)
Next
.Item(a(i, 2)) = w
Next
n = 1
For Each e In .keys
w = .Item(e)
For i = 1 To UBound(w, 2)
n = n + 1
For j = 1 To UBound(w, 1)
a(n, j) = w(j, i)
Next
Next
Next
.RemoveAll
For i = 2 To UBound(a, 1)
If Not .exists(a(i, 1)) Then
ReDim w(1 To UBound(a, 2), 1 To 1)
Else
w = .Item(a(i, 1))
ReDim Preserve w(1 To UBound(a, 2), 1 To UBound(w, 2) + 1)
End If
For j = 1 To UBound(a, 2)
w(j, UBound(w, 2)) = a(i, j)
Next
.Item(a(i, 1)) = w
Next
w = .items
End With
'Restitution
Application.ScreenUpdating = False
With .Offset(, .Columns.Count + 1)
.CurrentRegion.Clear
.Rows(1).Value = a: n = 2
For i = 0 To UBound(w)
With .Rows(n).Resize(UBound(w(i), 2))
.Value = Application.Transpose(w(i))
.Borders(xlEdgeBottom).Weight = xlMedium
End With
n = n + UBound(w(i), 2)
Next
With .CurrentRegion
.BorderAround Weight:=xlThin
.Borders(xlInsideVertical).Weight = xlThin
.Font.Name = "calibri"
.Font.Size = 10
.VerticalAlignment = xlCenter
.HorizontalAlignment = xlCenter
With .Rows(1)
.Font.Bold = True
.BorderAround Weight:=xlThin
.Cells(1).Interior.ColorIndex = 44
.Cells(2).Interior.ColorIndex = 43
End With
.Columns.AutoFit
End With
End With
End With
Application.ScreenUpdating = True
End Sub |