Sub MOformat()
Dim a, i As Long, ii As Long, e, w, n As Long
With Cells(1).CurrentRegion
a = .Value: ReDim w(1 To UBound(a, 2))
VSortM a, 2, UBound(a, 1), 4, 0
With CreateObject("Scripting.Dictionary")
For i = 2 To UBound(a, 1)
If a(i, 4) <> "" Then
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 ii = 1 To UBound(a, 2)
w(ii, UBound(w, 2)) = a(i, ii)
Next
.Item(a(i, 1)) = w
ElseIf a(i, 5) <> "" Then
If .exists(a(i, 1)) Then
w = .Item(a(i, 1))
For ii = 1 To UBound(w, 2)
If w(5, ii) = "" Then w(5, ii) = a(i, 5): Exit For
Next
.Item(a(i, 1)) = w
End If
End If
Next
n = 1
For Each e In .keys
w = .Item(e)
For i = 1 To UBound(w, 2)
n = n + 1
For ii = 1 To UBound(w, 1)
a(n, ii) = w(ii, i)
Next
Next
Next
End With
With .Offset(, .Columns.Count + 1).Resize(n)
.Value = a
.Offset(1).Sort .Cells(1), 1
End With
End With
End Sub
Sub VSortM(ary, LB, UB, ref, myOrd As Boolean)
Dim i As Long, ii As Long, iii As Long, M, temp
i = UB: ii = LB
M = ary(Int((LB + UB) / 2), ref)
Do While ii <= i
If myOrd Then
Do While ary(ii, ref) < M
ii = ii + 1
Loop
Do While ary(i, ref) > M
i = i - 1
Loop
Else
Do While ary(ii, ref) > M
ii = ii + 1
Loop
Do While ary(i, ref) < M
i = i - 1
Loop
End If
If ii <= i Then
For iii = LBound(ary, 2) To UBound(ary, 2)
temp = ary(ii, iii): ary(ii, iii) = ary(i, iii): ary(i, iii) = temp
Next
i = i - 1: ii = ii + 1
End If
Loop
If LB < i Then VSortM ary, LB, i, ref, myOrd
If ii < UB Then VSortM ary, ii, UB, ref, myOrd
End Sub
Display More