
'可以指定列次序排序,列数可以不限
Option Explicit
Sub abc()
Dim a, pos, i, j, k, p
a = [a1].CurrentRegion.Offset(1).Value
pos = Array(3, 4, 6, 5) 'C、D、F、E 列次序升序
Call bsort(a, 1, UBound(a, 1) - 1, 1, UBound(a, 2), pos(0))
For i = 1 To UBound(pos)
p = 0
For j = 1 To UBound(a, 1) - 1
For k = i - 1 To 0 Step -1
If a(j, pos(k)) <> a(j + 1, pos(k)) Then
Call bsort(a, p + 1, j, 1, UBound(a, 2), pos(i))
p = j: Exit For
End If
Next
Next
Next
[a1].Offset(1, UBound(a, 2) + 1).Resize(UBound(a, 1) - 1, UBound(a, 2)) = a
End Sub
Function bsort(a, 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 a(j, key) > a(j + 1, key) Then
For k = left To right
t = a(j, k): a(j, k) = a(j + 1, k): a(j + 1, k) = t
Next
End If
Next
Next
End Function