我有一张excel工作表,每列的标题都在第3行
我想知道(通过excel vba)如何根据标题名称按字母顺序重新排列列(列D-Z)。
感谢您的指导。
例如。在安排之前
. . . . . column D | column E | column F | column G | ...
row 1
row 2
row 3 zebra | car | monkey | balloon | ...
例如。重新安排后
. . . . . column D | column E | column F | column G | ...
row 1
row 2
row 3 balloon | car | monkey | zebra | ...
答案 0 :(得分:1)
您需要任何排序算法并将其应用于列(而不是行)
这是一个快速的&脏的(好吧,它不是一个超级快速的分拣机,只是出于我的记忆,但......):
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列。