Consulting

Results 1 to 7 of 7

Thread: Solved: Sorting Multiple Rows Horizontally

  1. #1

    Solved: Sorting Multiple Rows Horizontally

    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?

  2. #2
    VBAX Mentor
    Joined
    Dec 2008
    Posts
    404
    Location
    Sort each row separately ... or use the following macro:
    [vba]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[/vba]

    Artik

  3. #3
    Thanks, looks like the code worked perfectly. Now I just need to figure out how it worked.

  4. #4
    How do I mark it solved?

  5. #5
    VBAX Tutor
    Joined
    Dec 2008
    Posts
    244
    Location
    on the top of this page, thread tools --- > mark solved...

  6. #6
    Apologies for the delay. Mark solved does not appear in Google Chrome

  7. #7
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,058
    Location
    Consider it done NukedWhale
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •