您需要任何排序算法并将其应用于列(而不是行)
这是一个又快又脏的分拣机(好吧,这不是一个超级快速的分拣机,只是超出了我的记忆,但是......):
Sub HorSort(SortRange As Range, SortRow As Long)
Dim Idx As Long, Jdx As Long, Kdx As Long, Tmp As Variant
For Idx = 1 To (SortRange.Columns.Count - 1)
For Jdx = 1 To (SortRange.Columns.Count - 1)
' compare values in row to be sorted
If SortRange(SortRow, Jdx) > SortRange(SortRow, Jdx + 1) Then
' swap all cells in column with the one to the right
For Kdx = 1 To SortRange.Rows.Count
Tmp = SortRange(Kdx, Jdx)
SortRange(Kdx, Jdx) = SortRange(Kdx, Jdx + 1)
SortRange(Kdx, Jdx + 1) = Tmp
Next Kdx
End If
Next Jdx
Next Idx
End Sub
Sub Test()
HorSort Selection, 1
End Sub
在 A1 输入以下数据
5 2 4 1 3
A D B E C
1 2 3 4 5
选择A1..E3并运行每个
HorSort Selection, 1
HorSort Selection, 2
HorSort Selection, 3
从Sub Test()
. 您当然不限于 5 列。