按字母顺序重新排列工作表中特定范围的列

我有一个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 | ... 

您需要任何sortingalgorithm,并将其应用于列(而不是行)

这是一个快速和肮脏的(确定它不是一个超级快速的分拣机,只是我的记忆,但…):

 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处input以下数据

 5 2 4 1 3 ADBEC 1 2 3 4 5 

selectA1..E3并运行其中的每一个

 HorSort Selection, 1 HorSort Selection, 2 HorSort Selection, 3 

Sub Test() 。 你当然不限于5列。