Need macro to sort rows

  • Thread starter Thread starter Onfrek
  • Start date Start date
O

Onfrek

Hi, all!
I need macro that sort selected row and then jump to next row and so on
to the end of Sheet. I recorded this macro:

Sub Makro1()
Range("E2:H2").Select
Selection.Sort Key1:=Range("E2"), Order1:=xlAscending,
Header:=xlGuess, _
OrderCustom:=5, MatchCase:=False, Orientation:=xlLeftToRight
End Sub

This one sorts only one row, how can i solve my problem?
Thanks for any help
 
I used column E to determine the last row to sort.

Option Explicit
Sub Makro1A()

Dim LastRow As Long
Dim FirstRow As Long
Dim iRow As Long

With ActiveSheet
FirstRow = 2
LastRow = .Cells(.Rows.Count, "E").End(xlUp).Row
For iRow = FirstRow To LastRow
With .Cells(iRow, "E").Resize(1, 4)
.Sort key1:=.Columns(1), Order1:=xlAscending, Header:=xlNo, _
OrderCustom:=5, MatchCase:=False, _
Orientation:=xlLeftToRight
End With
Next iRow
End With

End Sub

..resize(1,4) says to take that range (whatever row, column E) and make it 1 row
high by 4 columns wide (E:H).

I changed the header to xlno. If you know whether you have headers, then it's
usually best to specify what you know.
 
Thanks Dave, it works!
The funny thing - for about two weeks ago I had the same problem and
found another solution(not so good as yours), but today I was not able
to do this again.

It was small macro that sorted one row and jumped then to next row and
stopped. I had to run it again and again to the last row, with shortcut
it was pretty quick. But your solution is much more better.
Regards, Onfrek
 
Back
Top