+ Reply to Thread
Results 1 to 15 of 15

Multi-level sorting button

  1. #1
    Registered User
    Join Date
    11-29-2019
    Location
    London, UK
    MS-Off Ver
    Office 365 Version 1910
    Posts
    8

    Multi-level sorting button

    Hi all

    Please note first of all, my VBA knowledge is zero, but I am pretty good with excel and I did programming a long time ago at college so I can understand stuff. I'm just not experienced.

    I have an Excel Table with a lot of columns (25 of them actually) and with a lot of items (about 2000 rows of data).

    The correct way to sort it is multi-level and requires 9 different sorts. This takes a while (a bunch of clicking), is prone to mistakes, and more importantly other users may not know the right way to do this. So I want a magic button that does it all.

    Before we get to the button itself (that's probably the easy part, although I've never done it), I started looking at the code that would do the sorting. I expected it to be pretty simple. With the help of Google and some YouTube videos, I ended up with this:

    Please Login or Register  to view this content.
    Please note:
    - the Workbook is called "Invoices & Cashflow.xlsm".
    - the Sheet on which the table is is called "Invoices" ("Sheet3" when looking at VBA dashboard)
    - the Table itself is also called "Invoices" (this is confirmed in the Name Manager)

    The error I am having is that it doesn't work. No matter how many new levels I add for sorting, only the first one works, i.e. the table gets sorted by "Cost Type" in ascending order, and everything stops there. This is of course not ideal. The last step should be "Payment Date", which supersedes all others.

    Why aren't the other levels working? "Cost Type" is only the first step out of 9 in total...

    Also, side note: I have no idea why ".Header = xlYes" is necessary, or what it does. I've noticed some online examples use it. It seems to make no difference to the result...

    Thanks for helping out, much appreciated.

    PS: any tips on the button are also appreciated, but I was going to search Google first before asking
    Last edited by MagellanOfBass; 11-29-2019 at 03:44 PM.

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Multi-level sorting button

    Welcome to the forum

    Please attach a sample workbook (not a picture or pasted copy). Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Forum Contributor
    Join Date
    10-30-2019
    Location
    Bistrita, Romania
    MS-Off Ver
    2019; 365
    Posts
    231

    Re: Multi-level sorting button

    I actually had such a code with just 2 levels of sorting but added 7 more, tested and the following is working for me.
    Of course, replace Table1 and it's columns [1], [2] ... etc. with your own.
    Please Login or Register  to view this content.
    To show your appreciation
    Click ★ Add reputation!

  4. #4
    Registered User
    Join Date
    11-29-2019
    Location
    London, UK
    MS-Off Ver
    Office 365 Version 1910
    Posts
    8

    Re: Multi-level sorting button

    Hi there, thanks for this! I tried it. It didn't work - same problem.

    In fact, I've got good news and bad news.

    The good news is, I didn't know Excel had this function already integrated!
    Data -> Sort. The Icon with the square, looks like conditional formatting window, can add multiple levels of sorting. Very handy.

    The bad news is, even after I set the whole 9 levels I want to do, correctly and as intended, I still get the same problem. Only the first level effectively gets sorted.

    Unfortunately the file has a lot of confidential info and it will take some time for me to edit it and be able to upload it here.

    In the meantime, if anyone knows about this strange error, please let me know ... Thanks

  5. #5
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Multi-level sorting button

    All we need are the headers in the right columns and rows.

    Also the type of data.

    So three rows in total

  6. #6
    Registered User
    Join Date
    11-29-2019
    Location
    London, UK
    MS-Off Ver
    Office 365 Version 1910
    Posts
    8

    Re: Multi-level sorting button

    Ok please see attached - does this work?

    I'm not using the actual workbook. I copied all the contents of that table and pasted VALUES ONLY into a new, blank workbook.

    This new workbook has a simple data set with simple filters... It is NOT a defined "table" (unlike the actual workbook I use).

    I've changed the name of 3 entities to random stuff, and left all the data pertaining to them ONLY. I've deleted all the other data. So there's only 3 "Counterparties".

    In this new workbook, with super simple formatting, no data validation, no conditions, no special anything, just plain data across 25 columns ... The sorting I want still doesn't work...

    See attached. Also an image of what I am doing. If you do it all manually, you get the result I want. If you do it via Excel's multi-level sorting, it doesn't work properly. The VBA solutions above also don't work.

    Thanks for looking into this!
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by MagellanOfBass; 11-29-2019 at 06:26 PM.

  7. #7
    Registered User
    Join Date
    11-29-2019
    Location
    London, UK
    MS-Off Ver
    Office 365 Version 1910
    Posts
    8

    Re: Multi-level sorting button

    Below 2 images, the first one showing how it looks like when I manually sort, and the second one when I use Excel's multi-level auto-sort (same criteria...)
    Attached Images Attached Images

  8. #8
    Registered User
    Join Date
    11-29-2019
    Location
    London, UK
    MS-Off Ver
    Office 365 Version 1910
    Posts
    8

    Re: Multi-level sorting button

    A friend suggested recording a macro of the sorting and testing it; if it works, then reviewing the code.

    Good news is, it worked.
    Bad news is, it's slow as hell. About 4x slower than manually clicking and sorting 9 times... The whole thing takes about 30 seconds... And when I tried it using the keybaord shortcut I designated for it, Excel froze. Lol. Tried this twice, froze on both occasions...

    Maybe the code is too overcomplicated and can be drastically simplified. But I'm no coder and this is only my second experience with VBA (first one was 7 years ago).

    If anyone could be so kind to have a look and clean it up, would be much appreciated.

    Please Login or Register  to view this content.
    Edit: at first glance, it appears that the below code can be removed 8 times, and only left once (the "Clear" command first, before any sorting, and the rest at the bottom, right before the end).

    Please Login or Register  to view this content.
    This made the code a lot simpler and smaller, but it did not work. Sorting did not work as expected (original error) ...
    Last edited by MagellanOfBass; 11-29-2019 at 07:40 PM.

  9. #9
    Valued Forum Contributor
    Join Date
    09-30-2018
    Location
    Vlaams Brabant Belgium
    MS-Off Ver
    365
    Posts
    456

    Re: Multi-level sorting button

    Hi MagellanOfBass,

    You order of sorting is wrong
    put then upside down and its what you want

    Attachment 652073

    hope that what you are looking for

    Grtz

  10. #10
    Registered User
    Join Date
    11-29-2019
    Location
    London, UK
    MS-Off Ver
    Office 365 Version 1910
    Posts
    8

    Re: Multi-level sorting button

    Hi Joske920

    Thanks but that attachment failed, it seems. Can't open anything.

    Also what do you mean "wrong" and "put then upside down"?

    Also why is this forum adding random asterisks* instead of spaces in my messages? Especially after the word "I". I have to edit each message. Annoying. :P

  11. #11
    Valued Forum Contributor
    Join Date
    09-30-2018
    Location
    Vlaams Brabant Belgium
    MS-Off Ver
    365
    Posts
    456

    Re: Multi-level sorting button

    Hi MagellanOfBass,

    this is what i mean

    Please Login or Register  to view this content.

    Grtz
    Last edited by Joske920; 11-29-2019 at 08:02 PM.

  12. #12
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Multi-level sorting button

    Post Deleted.
    Last edited by mehmetcik; 11-29-2019 at 08:52 PM.

  13. #13
    Registered User
    Join Date
    11-29-2019
    Location
    London, UK
    MS-Off Ver
    Office 365 Version 1910
    Posts
    8

    Re: Multi-level sorting button

    Hi Joske920

    That makes no sense... But guess what, I tried it, and it looks like it works!! And it's also quite fast...

    Can anybody explain why it works?

    Edit: it also works when using Excel's integrated multi-level sorting in inverse order. Why or how, I don't know. But the main takeaway is no VBA needed.
    Last edited by MagellanOfBass; 11-30-2019 at 06:15 AM.

  14. #14
    Valued Forum Contributor
    Join Date
    09-30-2018
    Location
    Vlaams Brabant Belgium
    MS-Off Ver
    365
    Posts
    456

    Re: Multi-level sorting button

    in manual mode you do all sorts 1 by 1 separate and in the multi level it sort on the columns in a whole

    if that makes sense

  15. #15
    Registered User
    Join Date
    11-29-2019
    Location
    London, UK
    MS-Off Ver
    Office 365 Version 1910
    Posts
    8

    Re: Multi-level sorting button

    Thanks again. To the mod - you could say that this issue is officially solved now. No VBA needed and button not required - excel already has all of that.

+ 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. Format the second level of multi-level category axis - clustered chart
    By mushkitoes in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 10-04-2018, 09:30 AM
  2. [SOLVED] 3-level sorting including sorting a column by the number of percent sign in each cell
    By terryhenderson in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-14-2017, 05:21 PM
  3. [SOLVED] creating Multi-level list styles using existing multi-level list?
    By JimmyWilliams in forum Word Formatting & General
    Replies: 2
    Last Post: 08-28-2017, 12:21 AM
  4. Multi-level Sorting Issue
    By Tejas.T in forum Excel General
    Replies: 5
    Last Post: 05-31-2014, 11:46 AM
  5. Bill of Materials conversion from multi level to single level
    By susmitpatel in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 07-11-2013, 12:53 AM
  6. Multi level BOM
    By neorez in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-23-2010, 05:22 PM
  7. Using macro to convert single level BOM to Multi Level BOM
    By andrew_chong in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-07-2006, 04:57 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