Option Explicit
Sub test()
Dim arr, sum, i, j, m
arr = [b2:c13]
Call dsort(arr, 1, UBound(arr, 1) - 1, 1, True)
For i = 1 To UBound(arr, 1) - 1
For j = i To UBound(arr, 1) - 1
sum = sum + arr(j, 2)
If arr(j, 1) <> arr(j + 1, 1) Then
m = m + 1: arr(m, 1) = arr(i, 1): arr(m, 2) = sum
sum = 0: i = j: Exit For
End If
Next j, i
[e11].Resize(m, UBound(arr, 2)) = arr
End Sub
Function dsort(arr, first, last, key, order)
Dim i, j, k, t
For i = first To last - 1
For j = i + 1 To last
If arr(i, key) < arr(j, key) Xor order Then
For k = 1 To UBound(arr, 2)
t = arr(i, k): arr(i, k) = arr(j, k): arr(j, k) = t
Next
End If
Next j, i
End Function |