+ Reply to Thread
Results 1 to 14 of 14

Sort columns in a predetermined order bases on headers (not alphabetic)

  1. #1
    Registered User
    Join Date
    10-30-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2010
    Posts
    24

    Sort columns in a predetermined order bases on headers (not alphabetic)

    Are there any way to sort headers in a predetermined way? Example
    Right now the order is:
    Candy
    Food
    Shoes
    Computers
    Shirts
    Towels

    Is there any way to sort them in another way? These are just made up categories in the real example there are around 30 different that has to be en a specific order (not alphabetic). So how can I do to sort the above mentioned in this order?:

    Food
    Computers
    Shoes
    Shirts
    Candy
    Towels

    Thanks in advance

  2. #2
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    25,411

    Re: Sort columns in a predetermined order bases on headers (not alphabetic)

    You could insert a new row 1, then enter numbers representing the sort order that you require, i.e. above Candy you would have 5, and 1 for Food, 3 for Shoes, 2 for Computers, 4 for Shirts and 6 for towels (and so on). Then highlight those columns and click on Data | Sort | Options and choose Sort Left to Right, then choose Row1 for the sort order, Smallest to Largest and click OK.

    Then you could delete row 1.

    Hope this helps.

    Pete

  3. #3
    Registered User
    Join Date
    10-30-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Sort columns in a predetermined order bases on headers (not alphabetic)

    Thanks Pete!

    I was thinking of doing that but the thing is that I am going to repeat this a lot of times with sheets that export from our CRM-system. In order to avoid as much manual work as possible, are there any way to code the predetermined order in VBA?

  4. #4
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Sort columns in a predetermined order bases on headers (not alphabetic)

    Daniel,

    or use

    Please Login or Register  to view this content.
    If you like my contribution click the star icon!

  5. #5
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Sort columns in a predetermined order bases on headers (not alphabetic)

    Using your example data the code would look like

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    10-30-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Sort columns in a predetermined order bases on headers (not alphabetic)

    Thank you very much OllieB

  7. #7
    Registered User
    Join Date
    10-30-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Sort columns in a predetermined order bases on headers (not alphabetic)

    OllieB

    What if i need 47 headers?

    I understand what to do with everuthing except for

    Please Login or Register  to view this content.
    And the files i am going to use this on will have different ranges
    Last edited by danieldaniel1; 11-05-2013 at 05:50 AM.

  8. #8
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Sort columns in a predetermined order bases on headers (not alphabetic)

    The .SetRange is the instruction that is used to specify the area (= range) to be sorted.

    So you will need to change that statement to reflect the area you want to be sorted on your data worksheet.

    In the example; the value 3 indicates the number of columns to be included (should thus become 47) and the value 6 should reflect the number of data rows on your worksheet (including the header row).

    You can also use something like (assuming the row number of the last data row is 101)

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    10-30-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Sort columns in a predetermined order bases on headers (not alphabetic)

    Can i use any lastrow function here?

  10. #10
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Sort columns in a predetermined order bases on headers (not alphabetic)

    Yes you can. Many different possibilities. For example

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    10-30-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Sort columns in a predetermined order bases on headers (not alphabetic)

    It says "Subscript out of range"

  12. #12
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Sort columns in a predetermined order bases on headers (not alphabetic)

    and where does it say that? (is the worksheet in question perhaps not called Sheet1?)

  13. #13
    Registered User
    Join Date
    10-30-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Sort columns in a predetermined order bases on headers (not alphabetic)

    That the strange thing. It is called Sheet1

  14. #14
    Registered User
    Join Date
    10-30-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Sort columns in a predetermined order bases on headers (not alphabetic)

    Now it works! Thank you so much Ollie!

+ 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. Alphabetic order
    By kuzna26 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-15-2013, 01:12 PM
  2. Arranging columns by name (non-alphabetic order)
    By mylovelyhorse in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-17-2012, 11:04 AM
  3. can i rearrange the following names in alphabetic order..
    By naughtyboy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-07-2006, 06:05 AM
  4. how do you sort two columns; data and headers
    By Bolek in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-19-2005, 01:25 PM
  5. moving cells to a predetermined order
    By Jonathan in forum Excel General
    Replies: 1
    Last Post: 09-22-2005, 07:05 PM

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