Assaf
Assaf

Reputation: 89

Sorting Range Left to Right from Row 2

I have 3 rows in excel with numbers that I want to sort left to right from smallest to largest values of Row 2 in VBA.

I found how to do it based on first row:

Range(Rows(1),Rows(3)).Sort Key1:=Range(Rows(1),Rows(3)), Order1:=xlAscending, Orientation:=xlLeftToRight

This is what I have:
5,6,2,1
4,3,2,1
a,d,w,x

This is what I want:
1,2,6,5
1,2,3,4
x,w,d,a

any ideas what little line of code i need to add to what I have that will make it work?

Upvotes: 0

Views: 84

Answers (1)

Mikku
Mikku

Reputation: 6654

Change Key1:=Range(Rows(1),Rows(3)) to Key1:=Rows(2)

Try:

Range(Rows(1), Rows(3)).Sort Key1:=Rows(2), Order1:=xlAscending, Orientation:=xlLeftToRight

Demo:

enter image description here

Upvotes: 1

Related Questions