2013-03-02 128 views
0

我正在尝试对选择中的每一行进行排序。例如: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新手。比我做错了?请帮忙。

回答

5

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

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

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

第一个是有道理的,因为你实际上排序行。第二个是我用宏录像机得到的。

相关问题