Excel VBA:对选择中的每一行进行排序

时间:2013-03-02 14:24:54

标签: excel vba excel-vba

我正在尝试对选择中的每一行进行排序。例如:

排序前

8 3 17
2 9 4
5 3 8
排序后

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的新手。比我做错了?请帮忙。

1 个答案:

答案 0 :(得分:5)

问题在于您的Orientation参数。它应该是xlSortRows或xlLeftToRight:

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

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

第一个是有意义的,因为您实际上正在排序行。第二个是我用宏录像机得到的。