0

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 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.