

Option Explicit
Sub 分段排序()
Dim a, i, j, m, n, p
a = Range("a2:h" & Cells(Rows.Count, "e").End(xlUp).Row + 1).Value
For i = 1 To UBound(a) - 1
For j = i To UBound(a) - 1
If Len(a(j, 4)) Then m = i: p = i: Exit For
Next
For j = j To UBound(a)
If Len(a(j, 4)) = 0 Then n = j - 1: Exit For
Next
Call bsort(a, m, n, 2, UBound(a, 2), 5)
For j = m To n
If a(j, 5) <> a(j + 1, 5) Then
Call bsort(a, p, j, 2, UBound(a, 2), 4)
p = j + 1
End If
Next
i = n + 1
DoEvents
Next
[a2].Offset(, UBound(a, 2) + 1).Resize(UBound(a) - 1, UBound(a, 2)) = a
End Sub
Function bsort(arr, first, last, left, right, key)
Dim i, j, k, t
For i = first To last - 1
For j = first To last + first - 1 - i
If arr(j, key) > arr(j + 1, key) Then
For k = left To right
t = arr(j, k): arr(j, k) = arr(j + 1, k): arr(j + 1, k) = t
Next
End If
Next
Next
End Function