+ Reply to Thread
Results 1 to 42 of 42

Re-order Table Columns using Sort Function

  1. #1
    Forum Contributor
    Join Date
    10-18-2012
    Location
    new york
    MS-Off Ver
    Office 365
    Posts
    177

    Re-order Table Columns using Sort Function

    I need to reorder the columns of a table and I am using the sort function to do this. The formula I am using is:

    =IF(ISBLANK(SORT(Table1[#All], 1, 1, TRUE)),"",SORT(Table1[#All], 1, 1, TRUE))

    It works but because the numbers in the original data are the table headers, it sorts 10 before 2 etc., presumably because it recognizes this as text rather than numbers. I can't figure out how to fix this. I need to keep the original data in table format for various reasons but I specifically put the numbers in the headers because I don't want to have to amend the formula every time (right now the formula is looking at "all of table 1" instead of A1 to F60 for example so if I add rows to table 1, the sort formula automatically includes them - hope this makes sense.)

    Can anyone help?

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,939

    Re: Re-order Table Columns using Sort Function

    Not sure, but did you try this?

    =IF(ISBLANK(SORT(Table1[All], 1, 1, TRUE)),"",SORT(Table1[All], 1, 1, TRUE))

    There are instructions at the top of the page explaining how to attach your sample workbook (yellow banner: HOW TO ATTACH YOUR SAMPLE WORKBOOK). Screenshots are of little practical use as we cannot manipulate them.

    A good sample workbook has just 10-20 rows of representative data that has been desensitised. It also has expected results mocked up, worked examples where required, relevant cells highlighted and a few explanatory notes.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  3. #3
    Forum Contributor
    Join Date
    10-18-2012
    Location
    new york
    MS-Off Ver
    Office 365
    Posts
    177

    Re: Re-order Table Columns using Sort Function

    Hi there,

    Thank you for your reply. No, that formula won't work. I have attached a sample workbook.

    Thank you
    Attached Files Attached Files

  4. #4
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,939

    Re: Re-order Table Columns using Sort Function

    Try this:

    =LET(h,--Table1[#Headers],b,VSTACK(h,Table1[#All]),DROP(SORTBY(b,INDEX(b,1),1),1))

    or this:

    =IF(Table1[#All]="","",LET(h,--Table1[#Headers],b,VSTACK(h,Table1[#All]),DROP(SORTBY(b,INDEX(b,1),1),1)))
    Attached Files Attached Files

  5. #5
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,939

    Re: Re-order Table Columns using Sort Function

    Any good to you?

    If that takes care of your original question, please choose Thread Tools from the menu link above and mark this thread as SOLVED.

    Also, if you have not already done so, you may not be aware that you can thank anyone who offered you help towards a solution for your issue by clicking the small star icon (* Add Reputation) located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of all those who offered help.

  6. #6
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,939

    Re: Re-order Table Columns using Sort Function

    Seen, but no reply. Not even one word of thanks.

  7. #7
    Forum Contributor
    Join Date
    10-18-2012
    Location
    new york
    MS-Off Ver
    Office 365
    Posts
    177

    Re: Re-order Table Columns using Sort Function

    Hello - that is because I am still trying to figure it out before posting my reply. It does not work when I paste it into my file and I'm not sure why.

  8. #8
    Forum Contributor
    Join Date
    10-18-2012
    Location
    new york
    MS-Off Ver
    Office 365
    Posts
    177

    Re: Re-order Table Columns using Sort Function

    I don't know how to make it work at all. When I double click on your formula in the sample workbook, it errors out.

    I also need to exclude columns that have an X in the header...I forgot about that part. Any ideas?

  9. #9
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,939

    Re: Re-order Table Columns using Sort Function

    Perhaps if you provide a more realistic set of data? I’ll have another look tomorrow - it’s evening here.

    If you need an explanation, then just ask. Clearly it works on the sample you gave us.

  10. #10
    Forum Contributor
    Join Date
    10-18-2012
    Location
    new york
    MS-Off Ver
    Office 365
    Posts
    177

    Re: Re-order Table Columns using Sort Function

    I know it works on the sample but it doesn't work when I copy and paste it into my workbook and when I double click and hit enter on the formula you wrote, it then errors out (despite it working when I open the file). I am not sure why that would be? Perhaps you know though!

    Are you upset that it took me just under an hour to reply to you? I am so very sorry - I am working on several things at once! Once I saw I couldn't get it to work, I tried a few things, then moved onto something else for a bit before attempting to try again and responding when I still couldn't get it to work. Thank you for helping me with this.

    I've attached the workbook again - I changed somethings in the raw data of the first table (not the headers) and your formulas errored out (I don't know why - I'm not suggesting they are incorrect, as they did work when I first opened the workbook). I also created a table underneath and the desired output beside it (reordered columns, columns with X in the headers removed).
    Attached Files Attached Files

  11. #11
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,939

    Re: Re-order Table Columns using Sort Function

    What error message do you get?

  12. #12
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Cool Re: Re-order Table Columns using Sort Function

    with Power Query

    1 3 10 2 4 5 7 6 9 8 1 2 3 4 5 6 7 8 9 10
    chocolate fridge
    10
    sweet confectionary treat yes
    1
    special chocolate sweet fridge confectionary yes treat special
    1
    10
    banana fruit bowl
    5
    sweet fruit not treat yes
    5
    regular banana sweet fruit bowl fruit yes not treat regular
    5
    5
    strawberry fridge
    6
    sweet fruit not treat no
    2
    regular strawberry sweet fridge fruit no not treat regular
    2
    6


    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by sandy666; 05-16-2023 at 04:09 PM. Reason: update

  13. #13
    Forum Contributor
    Join Date
    10-18-2012
    Location
    new york
    MS-Off Ver
    Office 365
    Posts
    177

    Re: Re-order Table Columns using Sort Function

    If you open the revised workbook I attached - does it show a #NAME? error? That is what I am getting!

  14. #14
    Forum Contributor
    Join Date
    10-18-2012
    Location
    new york
    MS-Off Ver
    Office 365
    Posts
    177

    Re: Re-order Table Columns using Sort Function

    Interesting! I have not used power query before but will play around and see if I can get it to work on my original data set...

  15. #15
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Re: Re-order Table Columns using Sort Function

    sure, for more info see Learn Power Query

  16. #16
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,939

    Re: Re-order Table Columns using Sort Function

    Quote Originally Posted by ea223 View Post
    If you open the revised workbook I attached - does it show a #NAME? error? That is what I am getting!
    This means that your version of 365 is not as recent as mine. Try running updates.

  17. #17
    Valued Forum Contributor
    Join Date
    11-27-2011
    Location
    usa
    MS-Off Ver
    Excel 2007, Excel 365
    Posts
    495

    Re: Re-order Table Columns using Sort Function

    The following is a formula for the OP that only uses 2 functions
    Formula: copy to clipboard
    Please Login or Register  to view this content.

  18. #18
    Forum Contributor
    Join Date
    10-18-2012
    Location
    new york
    MS-Off Ver
    Office 365
    Posts
    177

    Re: Re-order Table Columns using Sort Function

    Thank you! This almost worked really well but the only issue is it converts my numbers stored as text to actual numbers...which normally would be useful but in this case I actually need them as per the original. Do you know how to maintain the original formatting? Some of the numbers have 0s in front as well - I also need to maintain these!

  19. #19
    Forum Contributor
    Join Date
    10-18-2012
    Location
    new york
    MS-Off Ver
    Office 365
    Posts
    177

    Re: Re-order Table Columns using Sort Function

    This one didn't work for me! I got a NAME error again. But also based on the output I don't think it reordered the columns....not entirely sure though of course since it's all errors.

  20. #20
    Forum Contributor
    Join Date
    10-18-2012
    Location
    new york
    MS-Off Ver
    Office 365
    Posts
    177

    Re: Re-order Table Columns using Sort Function

    Unfortunately I am not an administrator - I'll see if they can do it for me soon!

  21. #21
    Valued Forum Contributor
    Join Date
    11-27-2011
    Location
    usa
    MS-Off Ver
    Excel 2007, Excel 365
    Posts
    495

    Re: Re-order Table Columns using Sort Function

    @ea223, you probably should indicate which post #s you are referring to when you comment on whether something worked or didn't work.

  22. #22
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,939

    Re: Re-order Table Columns using Sort Function

    @Johnny - your forum profile needs updating, please.

  23. #23
    Valued Forum Contributor
    Join Date
    11-27-2011
    Location
    usa
    MS-Off Ver
    Excel 2007, Excel 365
    Posts
    495

    Re: Re-order Table Columns using Sort Function

    @AliGW, anything in particular need updating?

  24. #24
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,939

    Re: Re-order Table Columns using Sort Function

    Yes. Your Excel version!

  25. #25
    Valued Forum Contributor
    Join Date
    11-27-2011
    Location
    usa
    MS-Off Ver
    Excel 2007, Excel 365
    Posts
    495

    Re: Re-order Table Columns using Sort Function

    @AliGW,

    My computer that I mainly use has Excel 2007 & I use it over 90% of the time, so that is what I would like any responses I receive, should I post a question about something, to be based on. The spouses computer, which I occasionally use has Excel 365.

    With that in mind, should I still update my profile? If Yes, should it be 2007 & 365, just 365 since that it the newest version that I occasionally use, or something else?

  26. #26
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,464

    Re: Re-order Table Columns using Sort Function

    If you have the functions, try:

    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  27. #27
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,464

    Re: Re-order Table Columns using Sort Function

    @johnnyL: I would suggest that you refer to both versions in your profile so everyone is aware of what you have access to. However, when you ask a question, state (in big bold letters) the version of Excel you need the solution to work for … or should I say "the version of Excel for which you need the solution to work"?

  28. #28
    Valued Forum Contributor
    Join Date
    11-27-2011
    Location
    usa
    MS-Off Ver
    Excel 2007, Excel 365
    Posts
    495

    Re: Re-order Table Columns using Sort Function

    Too Easy, Done.

  29. #29
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,464

    Re: Re-order Table Columns using Sort Function


  30. #30
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,939

    Re: Re-order Table Columns using Sort Function

    See if this works for you:

    =IF(Table1[#All]="","",LET(h,--Table1[#Headers],b,Table1[#All],SORTBY(b,h,1)))

    Look at AliGW 2 in the attached.

    SORTBY came out at the same time as SORT, so you should have it in your version.
    Attached Files Attached Files
    Last edited by AliGW; 05-17-2023 at 02:41 AM. Reason: Workbook added.

  31. #31
    Valued Forum Contributor
    Join Date
    06-23-2021
    Location
    Romania
    MS-Off Ver
    2021, 365 v 2208
    Posts
    722

    Re: Re-order Table Columns using Sort Function

    Hi, ea223
    Maybe this formula is working in your version. I hope it does because in the original post your formula is based on SORT function
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    If the new Header need to be text, just change the cell formatting from number to text
    Attached Files Attached Files
    Last edited by tanasedn; 05-17-2023 at 08:27 AM.
    Diana Tanase


    If the solutions offered helped you to solve your problem, then mark the thread as SOLVED (thread tools in the top menu) and you can click on * to add reputation to those who helped you, as a way to say thank you !

  32. #32
    Forum Contributor
    Join Date
    10-18-2012
    Location
    new york
    MS-Off Ver
    Office 365
    Posts
    177

    Re: Re-order Table Columns using Sort Function

    Thank you everyone for the responses - I will be back later to properly review.

  33. #33
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,939

    Re: Re-order Table Columns using Sort Function

    Glad to have helped.

    If that takes care of your original question once you have reviewed the suggestions, please choose Thread Tools from the menu link above and mark this thread as SOLVED.

    Also, if you have not already done so, you may not be aware that you can thank anyone who offered you help towards a solution for your issue by clicking the small star icon (* Add Reputation) located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of all those who offered help.

  34. #34
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,063

    Re: Re-order Table Columns using Sort Function

    Looking over this, I'd guess you don't have the most up-to-date version of O365. The up-to-dateness (ugh) varies depending on the exact product that you have.

    This WILL work in any O365 version AND keeps the headers as TEXT.

    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by Glenn Kennedy; 05-17-2023 at 11:45 AM.
    Glenn




    None of us get paid for helping you... we do this for fun. So DON'T FORGET to say "Thank You" to all who have freely given some of their time to help YOU.

    Temporary addition of accented to illustrate ongoing problem to the TT: L? fh?ile P?draig sona dhaoibh

  35. #35
    Forum Contributor
    Join Date
    10-18-2012
    Location
    new york
    MS-Off Ver
    Office 365
    Posts
    177

    Re: Re-order Table Columns using Sort Function

    @Glenn - thank you for this. This worked! Is there any way to return blank values as blanks instead of zeros, and not bring back any columns with an X in the heading?

  36. #36
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,063

    Re: Re-order Table Columns using Sort Function

    Format/Format cells/Custom

    0;-0;;@

    OK will hide zeros. It's a bit more problematic to do it by a formula as some of your columns are text and others are numbers.

    Columns containing X in the header... just omit them completely? I'm away for the night now, but can return to this tomorrow morning. Please post a sample file showing EXACTLY what sort of "headers with an X" that there might be. There may be some problems... but we'll see...

  37. #37
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,464

    Re: Re-order Table Columns using Sort Function

    To retain blanks:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

  38. #38
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,464

    Re: Re-order Table Columns using Sort Function

    @Glenn: the sample file in post #10 had a second table with Xs in the headings. It is Table13. Your existing solution works but has two columns on the end that have #N/A. Just need a tweak to dump (or hide) them.

    This will retain blanks and hide the two columns of #N/A results.
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Must be an aesthetically more pleasing solution that dumps them.

  39. #39
    Forum Contributor
    Join Date
    10-18-2012
    Location
    new york
    MS-Off Ver
    Office 365
    Posts
    177

    Re: Re-order Table Columns using Sort Function

    @TMS - I think this finally worked!! Thank you so much!

  40. #40
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,464

    Re: Re-order Table Columns using Sort Function

    You're welcome. Thanks for the rep.

    Assuming you have FILTER (pretty sure all versions of 365 have it) we can combine part of my original formula with Glenn's and remove the X columns and retain the blanks.

    Formula: copy to clipboard
    Please Login or Register  to view this content.

  41. #41
    Valued Forum Contributor
    Join Date
    11-27-2011
    Location
    usa
    MS-Off Ver
    Excel 2007, Excel 365
    Posts
    495

    Re: Re-order Table Columns using Sort Function

    Excel 365 should definitely have the FILTER function.

  42. #42
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,063

    Re: Re-order Table Columns using Sort Function

    I did it this way:

    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Attached Files Attached Files

+ 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. How to order columns of Pivot table in correct month order
    By Cordelia123 in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 04-23-2021, 08:29 AM
  2. Macro to sort multiple columns in order, on any sheet/table
    By Andrewjs in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-15-2017, 04:22 PM
  3. Pivot Table Sort Order, help please
    By Hassan1977 in forum Excel Charting & Pivots
    Replies: 3
    Last Post: 10-23-2015, 04:41 PM
  4. Sort two columns (names) with keeping data intregity and order within the columns
    By ikindaknow in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-15-2015, 10:01 AM
  5. Sort a large table in order
    By psgolfer32381 in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 02-11-2014, 04:45 PM
  6. Excel 2007 : Pivot Table Sort Order
    By paulrockliffe in forum Excel General
    Replies: 1
    Last Post: 03-30-2010, 12:46 PM
  7. Pivot table sort order
    By Vincdc in forum Excel General
    Replies: 2
    Last Post: 03-01-2006, 01:25 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