SOLVED

Excel 365, sorting rows of a range

Copper Contributor

I have a range of single digit numbers (please see below) and I want to sort each row Largest to Smallest (i.e., largest digits on the left and in descending order to the right). If I Custom sort each row it works, but I want to be able to sort a great number of rows in one go. How can I do this in Excel 365. Many thanks for your help.

100100
101103
101101
110002
001300
101202
002200
202012
001101
101002
003011
4 Replies

@star_man35 

You could adapt the following macro for your setup:

Sub SortRows()
    Dim r As Long
    Dim m As Long
    Application.ScreenUpdating = False
    m = Range("A" & Rows.Count).End(xlUp).Row
    For r = 1 To m
        Range("A" & r).EntireRow.Sort Key1:=Range("A" & r), Order1:=xlDescending, Header:=xlNo, Orientation:=xlSortRows
    Next r
    Application.ScreenUpdating = True
End Sub
Dear Mr. Hans Vogelaar,
Thank you very much for your quick response and for your kind advice. I shall try your micro and revert.
Thanks again
Harry Karanasssos
best response confirmed by star_man35 (Copper Contributor)
Solution

@star_man35 

If 'row' is a relative reference to a single row, then

= SORT(row,,-1,1)

will create a sorted copy of the row which may be filled down to complete the table.  To do the same thing with a single formula requires the latest version of 365

= MAKEARRAY(11,6,
      LAMBDA(r,c,
         INDEX(SORT(INDEX(data,r,),,-1,1),c)
      )
  )
Dear Mr. Peter Bartholomew,
Thank you very much for your quick advice and for the attachment that is extremely helpful. I did try to work around my problem using various 'Sort' variations, but Excel (in my opinion) does not like dealing with rows as much as it works well with columns.
Once again, very grateful for your help.
With kind regards,
Harry Karanassos
1 best response

Accepted Solutions
best response confirmed by star_man35 (Copper Contributor)
Solution

@star_man35 

If 'row' is a relative reference to a single row, then

= SORT(row,,-1,1)

will create a sorted copy of the row which may be filled down to complete the table.  To do the same thing with a single formula requires the latest version of 365

= MAKEARRAY(11,6,
      LAMBDA(r,c,
         INDEX(SORT(INDEX(data,r,),,-1,1),c)
      )
  )

View solution in original post