+ Reply to Thread
Results 1 to 6 of 6

Sort grouped data while preserving structure / hierarchy

  1. #1
    Registered User
    Join Date
    02-04-2012
    Location
    CA, USA
    MS-Off Ver
    Excel 2010
    Posts
    56

    Sort grouped data while preserving structure / hierarchy

    This seems to be a common problem, but I haven't found a solution, perhaps because I don't know what this type of sorting is officially called.

    This example should make it clear. I want to sort the following data, primary ascending on left column, secondary ascending on right, while maintaining the grouped structure....

    C 5
    7
    1
    A 3
    8
    6
    B 4
    2
    9

    .... so that the result is this...
    A 3
    6
    8
    B 2
    4
    9
    C 1
    5
    7

    .... NOT this:
    A 3
    B 4
    C 5
    1
    2
    6
    7
    8
    9

    My foremost concern is botching the data -- Once saved, the wrong sort per above is irreversible. Further, in my case there are thousands of rows, and nothing contextually associating the columns, so I won't be able to fix corrupted data even if trudging through it manually.

    Therefore, I don't want a method that fills cells or otherwise affects the original data, but rather puts the sorted data off to the side. A direct solution is of course preferred, but I'm open to helper columns, vba, multiple steps, etc.

    Any help appreciated!

  2. #2
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Sort grouped data while preserving structure / hierarchy

    First with this macro to fill the data in column A.

    After that just sort on column A (A-Z) and after that on column B (A - Z)

    See the attached file.

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Notice my main language is not English.

    I appreciate it, if you reply on my solution.

    If you are satisfied with the solution, please mark the question solved.

    You can add reputation by clicking on the star * add reputation.

  3. #3
    Registered User
    Join Date
    02-04-2012
    Location
    CA, USA
    MS-Off Ver
    Excel 2010
    Posts
    56

    Re: Sort grouped data while preserving structure / hierarchy

    The results are OK, but the macro altered the original data which now shows 4 A values and 2 C values instead of 3 values each.

    Is there a way to do it without filling or altering the original data, given the risk of corrupting it?

  4. #4
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Sort grouped data while preserving structure / hierarchy

    What a mess, I see that the data in the presented example is incorrect.

    The macro although works as expected.

    Try it on my excel file after you copied the data K1:L9 to A1:B9

    You will see the result is as expected.

  5. #5
    Registered User
    Join Date
    02-04-2012
    Location
    CA, USA
    MS-Off Ver
    Excel 2010
    Posts
    56

    Re: Sort grouped data while preserving structure / hierarchy

    The original data is critical and must not be altered. Per original post: "I don't want a method that fills cells or otherwise affects the original data."
    Yes, the original data can be duplicated to safely work on, but ideally I'd like to avoid that.

  6. #6
    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
    48,189

    Re: Sort grouped data while preserving structure / hierarchy

    You cannot sort the data without gap filling, adding a helper column, or copying the original data. In all cases, you'd add a formula in the blanks and then copy and paste special | values to make the inserts static.

    Regards, TMS
    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


+ 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. [SOLVED] Removing duplicate entries while preserving their structure
    By Lagrange17 in forum Excel General
    Replies: 5
    Last Post: 07-23-2014, 12:13 PM
  2. Creating a Pyramid Hierarchy structure from a flat structure
    By thegamerulez in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-16-2014, 01:28 AM
  3. Replies: 4
    Last Post: 12-12-2013, 09:49 AM
  4. CHeck boxes structure is changing when grouped into rows
    By kammariarun in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-01-2013, 07:56 PM
  5. present data as hierarchy/tree structure in user form
    By Malin in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-15-2005, 04:42 AM

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