Option Explicit
Sub test()
Dim arr, i, j, m, sum
arr = [c2].CurrentRegion.Offset(1).Resize(, 4).Value
ReDim sum(UBound(arr, 2))
Call qsort(arr, 1, UBound(arr, 1) - 1, 1, UBound(arr, 2), 1)
For i = 1 To UBound(arr, 1) - 1
For j = 2 To UBound(arr, 2)
sum(j) = sum(j) + arr(i, j)
Next
If arr(i, 1) <> arr(i + 1, 1) Then
m = m + 1: arr(m, 1) = arr(i, 1)
For j = 2 To UBound(arr, 2)
arr(m, j) = sum(j): sum(j) = 0
Next
End If
Next
[h3].Resize(m, UBound(arr, 2)) = arr
End Sub
Sub qsort(arr, first, last, left, right, key)
Dim i As Long, j As Long, k As Long, x, t
i = first: j = last: x = arr((first + last) \ 2, key)
While i <= j
While arr(i, key) < x: i = i + 1: Wend
While x < arr(j, key): j = j - 1: Wend
If i <= j Then
For k = left To right
t = arr(i, k): arr(i, k) = arr(j, k): arr(j, k) = t
Next
i = i + 1: j = j - 1
End If
Wend
If first < j Then qsort arr, first, j, left, right, key
If i < last Then qsort arr, i, last, left, right, key
End Sub |