- Sub test1()
- Dim ar, Dict As Object, i As Long, p As Long
- Set Dict = CreateObject("Scripting.Dictionary")
- ar = Split("姓名 性别 部门 次数")
- For i = 0 To UBound(ar)
- Dict(ar(i)) = UBound(ar) + 1 - i
- Next
- ar = Range("D1", Cells(Rows.Count, 1).End(xlUp).Offset(1))
- For i = 1 To UBound(ar) - 1
- If Dict.Exists(ar(i, 1)) Then p = i
- If Len(ar(i, 1)) And Len(ar(i + 1, 1)) = 0 Then
- QuickSort Dict, ar, p, i, 1, UBound(ar, 2), p
- End If
- Next
- Range("M1").Resize(UBound(ar), UBound(ar, 2)) = ar
- Set Dict = Nothing
- End Sub
- Function QuickSort(Dict As Object, ar, t As Long, b As Long, l As Long, r As Long, k As Long)
- Dim i As Long, j As Long, x As Long, y As Long, p As Long, v
- j = l
- x = r
- p = Dict(ar(k, (l + r) \ 2))
- While j <= x
- Do While j < r
- If Dict(ar(k, j)) > p Then j = j + 1 Else Exit Do
- Loop
- Do While x > l
- If p > Dict(ar(k, x)) Then x = x - 1 Else Exit Do
- Loop
- If j < x Then
- For y = t To b
- v = ar(y, j): ar(y, j) = ar(y, x): ar(y, x) = v
- Next
- j = j + 1: x = x - 1
- Else
- If j = x Then j = j + 1: x = x - 1
- End If
- Wend
- If l < x Then QuickSort Dict, ar, t, b, l, x, k
- If j < r Then QuickSort Dict, ar, t, b, j, r, k
- End Function
复制代码 |