- Sub test()
- Dim r%, i%, m%
- Dim arr, brr, zrr()
- With Worksheets("sheet1")
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- .Range("a2:e" & r).Sort key1:=.Range("a2"), order1:=xlAscending, key2:=.Range("c2"), order2:=xlAscending, Header:=xlNo
- arr = .Range("a2:e" & r)
- m = 0
- pm = Empty
- dj = Empty
- For i = 1 To UBound(arr)
- If arr(i, 1) <> pm Or Abs(arr(i, 3) - dj) > 1 Then
- m = m + 1
- ReDim Preserve zrr(1 To m)
- zrr(m) = Array(i, i)
- pm = arr(i, 1)
- dj = arr(zrr(m)(0), 3)
- Else
- If m > 0 Then
- zrr(m)(1) = i
- End If
- End If
- Next
- ReDim brr(1 To UBound(zrr), 1 To UBound(arr, 2))
- For k = 1 To UBound(zrr)
- brr(k, 1) = arr(zrr(k)(0), 1)
- brr(k, 2) = arr(zrr(k)(0), 2)
- For i = zrr(k)(0) To zrr(k)(1)
- brr(k, 4) = brr(k, 4) + arr(i, 4)
- brr(k, 5) = brr(k, 5) + arr(i, 5)
- Next
- brr(k, 3) = Round(brr(k, 5) / brr(k, 4), 3)
- Next
- .Range("h2").Resize(UBound(brr), UBound(brr, 2)) = brr
- End With
-
- End Sub
复制代码 |