PDA

View Full Version : Solved: Sorting Multiple Rows Horizontally



NukedWhale
01-07-2009, 01:38 PM
Hello,

I have a worksheet with several thousand rows of data. The particular data I need to sort is in 4 columns. Data is stored as numbers and there are occasional empty cells.

Using a comma as a delimiter, here's an example of what my data looks like

100,102,103,101
104,102,103,101
100,102,103,,
104,102,,,

I need to sort each row left to right in ascending order and I need each row to be sorted independently. Blank or empty cells should be placed to the right.

Excel has a built in horizontal sort ability but I can't sort multiple rows independently. By this I mean that if I select the entire array and sort by row 1, rows 2-2000 will be rearranged in the exact same fashion.

Can anyone help?

Artik
01-07-2009, 02:21 PM
Sort each row separately ;) ... or use the following macro: :)
Sub SortH()
Dim rng As Range
Dim rw As Range

If TypeName(Selection) <> "Range" Then Exit Sub

Set rng = Selection
If rng.Count = 1 Then
MsgBox "Select multiple cells!", vbExclamation
Exit Sub
End If

With Application
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With

For Each rw In rng.Rows
rw.Sort Key1:=rw.Cells(1, 1), _
Order1:=xlAscending, _
Header:=xlNo, _
Orientation:=xlSortRows
Next rw

With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = xlCalculationAutomatic
End With

End Sub

Artik

NukedWhale
01-07-2009, 04:55 PM
Thanks, looks like the code worked perfectly. Now I just need to figure out how it worked. :clap:

NukedWhale
01-12-2009, 08:21 AM
How do I mark it solved?

chungtinhlak
01-12-2009, 08:36 AM
on the top of this page, thread tools --- > mark solved... :)

NukedWhale
01-20-2009, 01:40 PM
Apologies for the delay. Mark solved does not appear in Google Chrome

Aussiebear
01-20-2009, 02:34 PM
Consider it done NukedWhale