+ Reply to Thread
Results 1 to 8 of 8

For Next Loop - Sort Each Row Left to Right

  1. #1
    Registered User
    Join Date
    04-15-2013
    Location
    new york city
    MS-Off Ver
    Excel 2010
    Posts
    24

    For Next Loop - Sort Each Row Left to Right

    Hello,

    I have a range of cells A1:D5 with letters from the alphabet - there are no column or row headers. I'd like the following code to start with Row 1 (A), sort the letters in that row from Left to Right, then go to Row 2 (B), repeat the same sort, and so on until all 5 rows are sorted.

    The following piece of code I've written will sort all 5 rows, but only based on what I set Key1 to. In it's current format, I thought it would have interpreted Cells (1,1) as the first cell in each row, but apparently it's being interpreted as only cell A1.

    Can someone please suggested a possible solution so that each row is sorted one-by-one? Thank you.

    Please Login or Register  to view this content.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: For Next Loop - Sort Each Row Left to Right

    Instead of Rng.Rows.Cells(1,1) try Rw.Cells(1,1).
    If posting code please use code tags, see here.

  3. #3
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127

    Re: For Next Loop - Sort Each Row Left to Right

    You're actually quite close. In the loop, RW references the range being sorted, so you do not need to use Rng. Try the following and report back.

    Please Login or Register  to view this content.

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: For Next Loop - Sort Each Row Left to Right

    Maybe:

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    04-15-2013
    Location
    new york city
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: For Next Loop - Sort Each Row Left to Right

    Hi Norie, I tried your suggestion, but this did not work. In my original code, the 1st row would sort alphabetically and all other rows would follow that same order. For example, imagine rows 1 and 2 looked like this:

    Row 1: D C B A
    Row 2: A B C D

    After I run my code, the rows would sort according to Row 1, Cells (1,1), and appear as such:

    Row 1: A B C D
    Row 2: D C B A

    Using your suggestion, the reverse happened and row 2 was sorted alphabetically whereas row 1 was sorted according to the original order of letters in row 2.

  6. #6
    Registered User
    Join Date
    04-15-2013
    Location
    new york city
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: For Next Loop - Sort Each Row Left to Right

    Hi BigBas, your slight tweak to Norie's suggestion was the last piece of the puzzle. The code now works! All the rows have been sorted alphabetically. Please see the correct code below (note: it is the same code BigBas posted above). Many thanks to you all!

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    04-15-2013
    Location
    new york city
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: For Next Loop - Sort Each Row Left to Right

    To clarify what piece of the code changed, please see below:

    Original code:
    Please Login or Register  to view this content.
    Correction to code
    Please Login or Register  to view this content.

  8. #8
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127

    Re: For Next Loop - Sort Each Row Left to Right

    No problem. Always keep in mind that while you're in a loop, reference the variable being used in the loop and not the entire Range object.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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