0
votes

I'm trying to sort each row in selection. Example:

before sorting:

8 3 17
2 9 4
5 3 8

after sorting:

3 8 17
2 4 9
3 5 8

I wrote this code, but it not works:

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

I'm newbie in VBA. Than I doing wrong? Please help.

1

1 Answers

5
votes

The issue is with your Orientation parameter. It should be either xlSortRows or xlLeftToRight:

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

or

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

The first one makes sense, as you are actually sorting the rows. The second is what I get with the macro recorder.