+ Reply to Thread
Results 1 to 6 of 6

Sort rows twice based on conditional

  1. #1
    Registered User
    Join Date
    05-13-2016
    Location
    San Fran, CA
    MS-Off Ver
    MS Office 2013
    Posts
    3

    Sort rows twice based on conditional

    Hi everyone,

    I am new to this forum and am already finding it pretty helpful. I am working trying to code a macro for the following. Any help will be appreciated.

    Input .csv file has

    SPlt SWell DPlt DWell Vol
    S5 C1 D1 F11 10
    S3 F12 D1 G02 10
    S2 C4 D1 C07 10
    S3 C4 D1 B08 10
    S5 D4 D1 E05 10
    S5 D8 D1 E07 10
    S3 F12 D1 F12 10
    S3 E3 D1 B05 10
    S5 E2 D1 B12 10
    S2 D8 D1 D09 10
    S3 D4 D1 D04 10
    S3 F12 D1 G06 10
    S5 B1 D1 C08 10
    S3 A11 D1 B03 10
    S5 E11 D1 B10 10
    S5 C3 D1 F08 10
    S3 A12 D1 A08 10
    S5 C5 D1 F06 10
    S5 E10 D1 B11 10
    S1 A1 D1 A02 10
    S3 D1 D1 D08 10
    S4 B7 D1 F03 10
    S4 A5 D1 C05 10
    S3 F12 D1 G03 10
    S3 A5 D1 C04 10

    First sort will have the rows sorted based on the first column. It will give result like this:
    SPlt Swell DPlt Dwell Vol
    S1 A1 D1 A02 10
    S2 C4 D1 C07 10
    S2 D8 D1 D09 10
    S3 F12 D1 G02 10
    S3 C4 D1 B08 10
    S3 F12 D1 F12 10
    S3 E3 D1 B05 10
    S3 D4 D1 D04 10
    S3 F12 D1 G06 10
    S3 A11 D1 B03 10
    S3 A12 D1 A08 10
    S3 D1 D1 D08 10
    S3 F12 D1 G03 10
    S3 A5 D1 C04 10
    S4 B7 D1 F03 10
    S4 A5 D1 C05 10
    S5 C1 D1 F11 10
    S5 D4 D1 E05 10
    S5 D8 D1 E07 10
    S5 E2 D1 B12 10
    S5 B1 D1 C08 10
    S5 E11 D1 B10 10
    S5 C3 D1 F08 10
    S5 C5 D1 F06 10
    S5 E10 D1 B11 10

    After grouping each SPlt (source plate), Then sort rows again based on the second column.
    I created a custom sort function for this (A1, B1, C1, D1, E1, F1, G1, H1, A2, B2 ... H12)

    The result should look like this:

    SPlt SWell DPlt DWell Vol
    S1 A1 D1 A02 10
    S2 C4 D1 C07 10
    S2 D8 D1 D09 10
    S3 D1 D1 D08 10
    S3 E3 D1 B05 10
    S3 C4 D1 B08 10
    S3 D4 D1 D04 10
    S3 A5 D1 C04 10
    S3 A11 D1 B03 10
    S3 A12 D1 A08 10
    S3 F12 D1 G02 10
    S3 F12 D1 F12 10
    S3 F12 D1 G06 10
    S3 F12 D1 G03 10
    S4 A5 D1 C05 10
    S4 B7 D1 F03 10
    S5 B1 D1 C08 10
    S5 C1 D1 F11 10
    S5 E2 D1 B12 10
    S5 C3 D1 F08 10
    S5 D4 D1 E05 10
    S5 C5 D1 F06 10
    S5 D8 D1 E07 10
    S5 E10 D1 B11 10
    S5 E11 D1 B10 10

    I tried could not get it to work given the complexity of implementing in the code. Please help!

    Thanks,
    Forum

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

    Re: Sort rows twice based on conditional

    I seriously do not understand.

    You can sort by Column A and then by column B as one instruction.

    But that will not result in the output you show.
    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
    Registered User
    Join Date
    05-13-2016
    Location
    San Fran, CA
    MS-Off Ver
    MS Office 2013
    Posts
    3

    Re: Sort rows twice based on conditional

    Hi mehmetcik,

    Thank you for taking a look at the post.

    In the excel I was able to achieve this result by: Performing first sort based on column A, then inserting a blank row between each source plate and then just selecting the grouped rows and using sort function again with the custom sort list that I mentioned above that yielded the results.

    However, the program where I would be using this code the csv file is unopened so need to perform this using VB code.

    I have been able to figure out code to sort the rows using column A and insert the blank rows: It is as follows:

    Comment: Sort the sheet based on the first column (source)

    Please Login or Register  to view this content.
    Comment: Insert a blank row to separate out the source

    Please Login or Register  to view this content.
    I am still in teh process of figuring out the second sort. Please let me know if you are still unclear on what I'm trying to do here.

    Thanks
    Last edited by fshah24; 05-13-2016 at 05:39 PM. Reason: Add # to wrap the code

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

    Re: Sort rows twice based on conditional

    I am sorry but I have to advise you:

    Please edit your last post.



    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)

  5. #5
    Registered User
    Join Date
    05-13-2016
    Location
    San Fran, CA
    MS-Off Ver
    MS Office 2013
    Posts
    3

    Re: Sort rows twice based on conditional

    Done. I apologize for that.

    Forum

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

    Re: Sort rows twice based on conditional

    You do not need to use loops to insert the new rows.

    If you have a lot of data the loops will really slow the macro down

    This code does the first sort and inserts the rows:

    You need t explain the next stage for me.

    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)

Similar Threads

  1. Replies: 2
    Last Post: 11-05-2015, 02:46 AM
  2. Sort rows based on same value
    By TomMichels in forum Excel General
    Replies: 1
    Last Post: 07-16-2014, 07:59 AM
  3. [SOLVED] I have a sort macro. How to add script to preselect rows to sort based on column value?
    By Jasonhouse in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-09-2014, 02:25 AM
  4. [SOLVED] How to sort rows based on their date?
    By dbwoods11 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-16-2013, 01:06 AM
  5. Copy and Paste Rows based on Criteria onto another sheet and sort based on oldest item
    By Kushal8684 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-25-2013, 05:37 AM
  6. Excel 2007 : sort rows conditional of cell content
    By grsnipe in forum Excel General
    Replies: 5
    Last Post: 10-12-2010, 09:09 AM
  7. Select rows and sort based on type
    By Sarah in forum Excel General
    Replies: 0
    Last Post: 10-11-2005, 01:05 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