+ Reply to Thread
Results 1 to 9 of 9

Programmatically sort rows separately based on value in cell?

  1. #1
    Registered User
    Join Date
    01-13-2009
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    4

    Programmatically sort rows separately based on value in cell?

    I'm banging my head against the wall trying to figure this one out, can anyone help?

    My spreadsheet has many columns, one of which is sort of an ID Code or flag column (column B). For one of the views of this data, I need to treat rows differently based on the value of the ID Code in column B.

    So, for example, say the ID codes are X, Y, Z, or no id (null). I need to be able to pull all of the rows with ID code X to the top of the list; the remaining codes can go back in amongst those with no code for sorting. After I do that and get the X's at the top of the list, I need to be able to apply my primary sort first to the list of X's and then to the remaining list. The goal is to have all of the X's at the top with the chosen sort applied, and everything remaining below with the same chosen sort applied.

    I will need to set this up to sort on a number of different criteria, so I am hoping to set this up programmatically rather than having to do it manually each time. I've got some idea of the logic but need help with the code.
    • So first, I'm sorting by column B to get my X's to the top.
    • Next I'm finding the last row with an X in column B, and applying the sort to that range.
    • Then I'm finding the last row with an X in column B plus one, and applying the sort from there to the end of the range (last row with any data at all)

    Does that seem right? Is this ridiculously crazy or impossible to do?
    Last edited by VBA Noob; 01-13-2009 at 04:01 PM.

  2. #2
    Forum Contributor
    Join Date
    04-21-2007
    Location
    Lima, Peru
    MS-Off Ver
    2000, 2007, 2010
    Posts
    674

    Sorting ranges

    Hi

    It should be straightforward to do using a helper column as follows

    1. Use the FIND function to detect each matching row and insert a value in the helper column (e.g 1, x or whatever).

    2. Sort the rows on the helper column.

    3. Sort the range using the helper column count to determine range size.

    4. Delete helper column contents

    If you have an example please post and I will add some code for you.

    regards

    Jeff

  3. #3
    Registered User
    Join Date
    01-13-2009
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    4
    Thanks Jeff,

    That is a great idea! Unfortunately I don't know that adding a column is something I can feasibly implement. The spreadsheet has 100+ columns and several other sheets in the workbook, with dozens of formulas and lookups across the sheets. Adding a column would be a major undertaking that would have to be reflected in many other places. Additionally, this column was originally intended to be the helper column for another column, so it's starting to get a little too recursive! If it's the only way to solve the problem I'm willing to entertain it but it almost seems easier to continue doing the sort manually.

    Is there another way I should think about this? Can I use a match to find all the rows with my ID code and apply a sort, then use a match to find the remaining rows and apply the sort? I'm not as familiar with that so I wouldn't exactly know how to go about it.

    Thanks!

  4. #4
    Forum Contributor
    Join Date
    04-21-2007
    Location
    Lima, Peru
    MS-Off Ver
    2000, 2007, 2010
    Posts
    674

    Sorting Ranges

    Hi

    In reality you are not adding a column physically, it can be any blank column and does not reflect at all on anything else in the spreadsheet. The main reason to use it is that all relevant data can be simply brought to the top of the sheet for easier viewing. It is also possible to do it without the column and data would then remain in whatever position it was sorted to alphabetically.

    Regards

    Jeff

  5. #5
    Valued Forum Contributor mdbct's Avatar
    Join Date
    11-11-2005
    Location
    CT
    MS-Off Ver
    2003 & 2007
    Posts
    848
    The following uses a bubble sort to move the rows with an "X" to the top of the sheet. It then sorts those rows based on the C column. Lastly, it sorts the remaining rows based on the C column as well.

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    01-13-2009
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    4
    Thank you! I will try this and report back.

  7. #7
    Valued Forum Contributor mdbct's Avatar
    Join Date
    11-11-2005
    Location
    CT
    MS-Off Ver
    2003 & 2007
    Posts
    848
    Here is a slightly different version. It should run much faster with larger data sets. It only runs through the rows once instead of the number of rows squared - 10 rows of data would force 100 loops of the bubble routine.

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    01-13-2009
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    4
    mdbct,

    I am having a bit of trouble getting this to work. My data does not begin until row 19, so I have substituted that for 2 in the row number. I do not get any compile errors. However, when I run the sort all that happens is that rows 2 through 19 are selected (nothing is sorted).

    How should I modify this code assuming my data begins at row 19 (that is the first row with data; the header row data is contained in rows 1 through 18)?

    Also, if I wanted to sort by a secondary criteria after sorting by row C, is that possible?

    Thank you for your continued assistance. I really appreciate it!

    Brianala

  9. #9
    Valued Forum Contributor mdbct's Avatar
    Join Date
    11-11-2005
    Location
    CT
    MS-Off Ver
    2003 & 2007
    Posts
    848
    Try this version. I've highlighted the code section for the secondary sort. In this case, the D column, descending. I also realized I could eliminate one of the looping sections as well.

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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