Excel VBA:对选中的每一行进行sorting

我试图对选中的每一行进行sorting。 例:

在sorting之前:

8 3 17 2 9 4 5 3 8 

sorting后:

 3 8 17 2 4 9 3 5 8 

我写了这段代码,但是不起作用:

 Public Sub SortByString() Dim row As Range For Each row In Selection.Rows row.Sort Key1:=row, Order1:=xlAscending, Orientation:=xlSortColumns Next row End Sub 

我是VBA的新手。 比我做错了吗? 请帮忙。

问题与您的方向参数。 它应该是xlSortRows或xlLeftToRight:

 row.Sort Key1:=row, Order1:=xlAscending, Orientation:=xlSortRows 

要么

 row.Sort Key1:=row, Order1:=xlAscending, Orientation:=xlLeftToRight 

第一个是有道理的,因为你实际上是sorting行。 第二个是我用macroslogging器得到的。