Sorting row data?!

  • Thread starter Thread starter Jo
  • Start date Start date
J

Jo

Hi everyone,

I know how to sort data by row, but say I have 5000 or more rows!
Going to each row and doing the sorting is time-consuming.

Is there a smart way to do this one time?

Thanks,
Jo
 
If i get what you are asking select the area you want to sort, go to Data->
Sort select Options and change the Orientation box to Left and Right then
select your row to sort by.
 
I asssume you want to sort each row, from left to right and that you're
looking for a macro to do that. Here's one way:

Sub test()
Dim r As Range

For Each r In ActiveSheet.UsedRange.Rows
r.Sort key1:=r.Cells(1, 1), Order1:=xlAscending,
Orientation:=xlLeftToRight
Next r
End Sub
 
If i get what you are asking select the area you want to sort, go to Data->
Sort select Options and change the Orientation box to Left and Right then
select your row to sort by.
--
-John
Please rate when your question is answered to help us and others know what
is helpful.








- Show quoted text -

John,

That what I did with one row, but how it can be done for 5000 rows;
each row by itself.....it is none sens to do it 5000 times
 
I asssume you want to sort each row, from left to right and that you're
looking for a macro to do that. Here's one way:

Sub test()
Dim r As Range

For Each r In ActiveSheet.UsedRange.Rows
r.Sort key1:=r.Cells(1, 1), Order1:=xlAscending,
Orientation:=xlLeftToRight
Next r
End Sub

--
Hope that helps.

Vergel Adriano








- Show quoted text -

Vergel,

Are you sure it is correct? I am getting these two rows in RED:

r.Sort key1:=r.Cells(1, 1), Order1:=xlAscending,
Orientation:=xlLeftToRight
 
Jo,

The newsreader wrapped the text to the next line. Make sure you put this in
just one line:

r.Sort key1:=r.Cells(1, 1), Order1:=xlAscending, Orientation:=xlLeftToRight
 
Jo,

The newsreader wrapped the text to the next line. Make sure you put this in
just one line:

r.Sort key1:=r.Cells(1, 1), Order1:=xlAscending, Orientation:=xlLeftToRight

--
Hope that helps.

Vergel Adriano






- Show quoted text -

What if I don't want the whole row but a range of rows I highlight
1st?

Reason is the 1st cell in in each row is somehow an ID code (number
too) which I don't sorting to touch!......
 
Jo,

To skip the first cell of each row, do it this way:

Sub test()
Dim r As Range

For Each r In ActiveSheet.UsedRange.Rows
r.Offset(0, 1).Sort key1:=r.Cells(1, 1), Order1:=xlAscending,
Header:=xlYes, Orientation:=xlLeftToRight
Next r
End Sub


To sort the rows in a range that you will select first, then this way:

Sub test2()
Dim r As Range

For Each r In Selection.Rows
r.Sort key1:=r.Cells(1, 1), Order1:=xlAscending,
Orientation:=xlLeftToRight
Next r
End Sub
 
Jo,

To skip the first cell of each row, do it this way:

Sub test()
Dim r As Range

For Each r In ActiveSheet.UsedRange.Rows
r.Offset(0, 1).Sort key1:=r.Cells(1, 1), Order1:=xlAscending,
Header:=xlYes, Orientation:=xlLeftToRight
Next r
End Sub

To sort the rows in a range that you will select first, then this way:

Sub test2()
Dim r As Range

For Each r In Selection.Rows
r.Sort key1:=r.Cells(1, 1), Order1:=xlAscending,
Orientation:=xlLeftToRight
Next r
End Sub

--
Hope that helps.

Vergel Adriano







- Show quoted text -

Yes it helps, thanks. One more question, say after sorting I want to
no repetitive values per row. So, If after sorting I get 12, 12, 13,
14 I want the result to be 12, 13, 14.......what else I should add to
the code to add this feature? Thanks again....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top