+ Reply to Thread
Results 1 to 2 of 2

Sort multiple rows by data in certain columns

  1. #1
    lbabli
    Guest

    Sort multiple rows by data in certain columns

    I want to sort only certain columns (or cells) in a row as follows:

    Unsorted...
    A B C D E
    1 Label3 1 3 4 2
    2 Label2 4 1 16 8
    3 Label4 2 9 1 6

    Desired Sort of cells in columns B, C, D & E...sorting left to right...
    A B C D E
    1 Label3 1 2 3 4
    2 Label2 1 4 8 16
    3 Label4 1 2 6 9

    Any idea how this can be done?

    Thank you

  2. #2
    Dave Peterson
    Guest

    Re: Sort multiple rows by data in certain columns

    If you select B2:E2, you can use Data|sort and click on the Options button.

    From there, you can specify that you want to sort by rows.

    In code:

    Option Explicit
    Sub testme01()

    Dim myRng As Range
    Dim iRow As Long
    Dim FirstRow As Long
    Dim LastRow As Long
    Dim wks As Worksheet

    Set wks = Worksheets("sheet1")

    With wks
    FirstRow = 2 'headers in row 1??
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

    For iRow = FirstRow To LastRow
    Set myRng = .Range(.Cells(iRow, "B"), _
    .Cells(iRow, .Columns.Count).End(xlToLeft))
    myRng.Sort key1:=myRng.Cells(1), order1:=xlAscending, _
    Header:=xlNo, Orientation:=xlLeftToRight
    Next iRow
    End With
    End Sub

    If you're new to macros, you may want to read David McRitchie's intro at:
    http://www.mvps.org/dmcritchie/excel/getstarted.htm
    lbabli wrote:
    >
    > I want to sort only certain columns (or cells) in a row as follows:
    >
    > Unsorted...
    > A B C D E
    > 1 Label3 1 3 4 2
    > 2 Label2 4 1 16 8
    > 3 Label4 2 9 1 6
    >
    > Desired Sort of cells in columns B, C, D & E...sorting left to
    > right...
    > A B C D E
    > 1 Label3 1 2 3 4
    > 2 Label2 1 4 8 16
    > 3 Label4 1 2 6 9
    >
    > Any idea how this can be done?
    >
    > Thank you
    >
    > --
    > lbabli
    >
    > ------------------------------------------------------------------------
    > lbabli's Profile: http://www.excelforum.com/member.php...fo&userid=3952
    > View this thread: http://www.excelforum.com/showthread...hreadid=480196


    --

    Dave Peterson

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1