+ Reply to Thread
Results 1 to 8 of 8

Sorting rows and columns with VBA

  1. #1
    Forum Contributor
    Join Date
    06-24-2013
    Location
    New Zealand
    MS-Off Ver
    Excel 2016
    Posts
    124

    Sorting rows and columns with VBA

    Hi there

    I have an issue with sorting rows / cols...
    My aim is to sort columns from smallest to largest but keep this by row.

    Example:
    Row before change: 27 | 32 | 01 | 34
    Row after change : 01 | 27 | 32 | 34


    Here is my code so far however the it requires a loop which is not really the best option.

    Please Login or Register  to view this content.

    Any help would be really great

    Thanks
    D

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Sorting rows and columns with VBA

    Hello DanzaNZ,

    You can sort the rows (horizontal data) by declaring the entire range as an object rather than selecting each of the rows individually. In VBA it is rare that you need to select anything before an operation is performed. Selecting also increases the macros execution time.

    If you can post your workbook that would best. It will allow solutions to be tested directly for correctness.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Contributor
    Join Date
    06-24-2013
    Location
    New Zealand
    MS-Off Ver
    Excel 2016
    Posts
    124

    Re: Sorting rows and columns with VBA

    Hi Leith Ross

    Thanks for the reply.
    A sample is attached
    Attached Files Attached Files

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Sorting rows and columns with VBA

    Hello DanaNZ,

    I was wrong. In Excel 2007 through 2016 the maximum number of sort fields in a single sort operation is 64. The number of sorts is unlimited if you only use one sort field at a time in a loop.

    The odd thing was even my code would sort only one row at a time using VBA. However, selecting the range in Excel and using the context menu to sort would sort all the rows correctly.

    I have been looking for any other reports of this problem. So far, I found no references to it but will keep looking.

  5. #5
    Forum Contributor
    Join Date
    06-24-2013
    Location
    New Zealand
    MS-Off Ver
    Excel 2016
    Posts
    124

    Re: Sorting rows and columns with VBA

    Hi Leith Ross

    Thank you for looking into this.
    I've searched high and low for something suitable however the loop looks to be the only thing that works.
    At the moment I use a vbs code to run the macro without opening the workbook, which helps with the screen updating rubbish.
    Maybe there is a away to sort the rows using vbs without macros from the workbook. How is your knowledge with vbs? Mine is pretty basic.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Sorting rows and columns with VBA

    Hello DanaNZ,

    It seems you are correct that using a loop is the only with the rows. I did create a loop that works well for sorting the rows. A button has been added to the sheet to run it.

    In the attached workbook, your code has been moved to Module1 as not to interfere with macro called by the buttons. Here is the macro code. Try this and let me know if this will work for your needs.

    Sort By Rows Macro
    Please Login or Register  to view this content.
    Attached Files Attached Files

  7. #7
    Forum Contributor
    Join Date
    06-24-2013
    Location
    New Zealand
    MS-Off Ver
    Excel 2016
    Posts
    124

    Re: Sorting rows and columns with VBA

    Great!

    Works really well, thank you for your help.

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Sorting rows and columns with VBA

    Hello DanaNZ,

    You're welcome. I learned something new today.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Sorting rows and columns.
    By adamkerr in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-16-2015, 04:54 PM
  2. VBA for sorting columns and rows
    By vietafghantx in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-29-2014, 08:40 PM
  3. [SOLVED] alphabetic sorting of 4 columns when one of the columns has 2 rows
    By jnribbit in forum Excel General
    Replies: 10
    Last Post: 08-05-2013, 09:45 PM
  4. Sorting columns by rows
    By transportplanner in forum Excel General
    Replies: 11
    Last Post: 04-21-2010, 09:26 AM
  5. sorting out columns but only for some rows
    By Sam in forum Excel General
    Replies: 5
    Last Post: 04-23-2005, 11:06 AM
  6. [SOLVED] How to keep rows together when sorting columns?
    By Amit in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-28-2005, 01:06 PM
  7. [SOLVED] Sorting rows AND columns
    By 3D in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-06-2005, 11:06 PM

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