+ Reply to Thread
Results 1 to 15 of 15

sort rows by pairs of columns

  1. #1
    Registered User
    Join Date
    09-24-2020
    Location
    Athens
    MS-Off Ver
    2013
    Posts
    13

    sort rows by pairs of columns

    Hello everyone,

    I am trying to record a macro that will sort my data by rows and keep columns in pairs of 2.

    for example,

    unit1 unit2 unit3
    quantity price Quantity price Quantity price
    100 30 200 28 500 22
    100 39 200 26 500 2
    100 333 200 26 500 255
    100 29 200 26 500 33

    And i want to sort every row based on the price of each pair. transform it to this:

    unit1 unit2 unit3
    quantity price Quantity price Quantity price
    500 22 200 28 100 30
    500 2 200 26 100 39
    200 26 500 255 100 333
    200 26 100 29 500 33

    my code and sample workbook are attached but here is also what i have accomplished so far.
    My code works for sorting rows but it doesn't give me the pairing sorting i want.

    Please Login or Register  to view this content.

    Thanks in advance.

    Any help is more than welcome
    Attached Files Attached Files
    Last edited by jiminho13; 09-25-2020 at 12:26 PM.

  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 by pairs of columns

    Administrative Note:

    Greetings from London.

    The good news is what you ask for is quite simple.

    The Bad News is we would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then 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

    (Note: this change is not optional. No help to be offered until this moderation request has been fulfilled.)
    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
    09-24-2020
    Location
    Athens
    MS-Off Ver
    2013
    Posts
    13

    Re: sort rows by pairs of columns

    Thank you for the clarification,

    I proceed by posting the code with the appropriate format for the query displayed above:

    Please Login or Register  to view this content.

  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 by pairs of columns

    Thanks.

    Try this code:

    Please Login or Register  to view this content.


    This is shorter and probably faster:

    Please Login or Register  to view this content.
    Last edited by mehmetcik; 09-26-2020 at 11:08 AM.

  5. #5
    Registered User
    Join Date
    09-24-2020
    Location
    Athens
    MS-Off Ver
    2013
    Posts
    13

    Re: sort rows by pairs of columns

    I would like to thank you again for your prompt response to my issue.

    At first glance, it looked fine and working perfectly.

    I would like to point a few issues that i discovered in the process.

    Firstly, if i run the code and then change some values and try to execute the code again it doesn't sort the values at all.

    Secondly, it doesn't look to sort every value i choose to input.

    For example if i apply the code to this dataset:

    Attachment 697267

    the outcome i get is this:

    Attachment 697268

    It seems it doesn't sort all the rows correctly. I would also like to have the option to add more pairs of columns to the dataset.

    The sample workbook is attached.

    Thank you in advance for any further help you may provide!
    Attached Files Attached Files
    Last edited by jiminho13; 09-28-2020 at 07:56 AM.

  6. #6
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,577

    Re: sort rows by pairs of columns

    Here is the code . Even if duplicates are there code works.

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Pl note
    Array formula should be confirmed with Ctrl+Shift+Enter keys together.
    If answere is satisfactory press * to add reputation.

  7. #7
    Registered User
    Join Date
    09-24-2020
    Location
    Athens
    MS-Off Ver
    2013
    Posts
    13

    Re: sort rows by pairs of columns

    Thank you for your addition!

    Although the code works and sorts the pairs for every row from the pair with the lowest price to the pair with the highest price, i actually need to expand it for many more pairs which means many more columns. I am not sure how your approach might be modified to implement this particularity.

    Thanks again!

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

    Re: sort rows by pairs of columns

    Hi

    I misunderstood your requirement.

    I was sorting by the first column of the pair. You need it sorted by the second column of the pair.

    Please Login or Register  to view this content.
    Last edited by mehmetcik; 09-28-2020 at 04:04 PM.

  9. #9
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,577

    Re: sort rows by pairs of columns

    here is the new code. To change the columns change this line in code suitably.
    Line
    Please Login or Register  to view this content.
    Code for macro
    Please Login or Register  to view this content.
    Attached Files Attached Files

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,516

    Re: sort rows by pairs of columns

    Simply
    Please Login or Register  to view this content.
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    09-24-2020
    Location
    Athens
    MS-Off Ver
    2013
    Posts
    13

    Re: sort rows by pairs of columns

    Thank you mehmetcik for your response once again!

    Is there a way that this macro could work if price has decimals?

    It works perfectly for whole numbers but when i use decimal prices (the column that the pairs are sorted) it gets me the subscript out of range error.

    Thanks again!

  12. #12
    Registered User
    Join Date
    09-24-2020
    Location
    Athens
    MS-Off Ver
    2013
    Posts
    13

    Re: sort rows by pairs of columns

    Many many thanks for the macro kvsrinivasamurthy,

    There is the same issue here, when i use decimal numbers in price column i get error.

    Kind regards.

  13. #13
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,577

    Re: sort rows by pairs of columns

    To avoid decimal problem in the code change this line
    Please Login or Register  to view this content.
    as
    Please Login or Register  to view this content.
    and this line
    Please Login or Register  to view this content.
    as
    Please Login or Register  to view this content.
    Last edited by kvsrinivasamurthy; 09-29-2020 at 10:51 AM.

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

    Re: sort rows by pairs of columns

    Please follow kvsrinivasamurthy's advice.

    my code works for decimals in the price column.

  15. #15
    Registered User
    Join Date
    09-24-2020
    Location
    Athens
    MS-Off Ver
    2013
    Posts
    13

    Re: sort rows by pairs of columns

    Thank you very much for your generosity kvsrinivasamurthy . It works very well but when i have many columns and rows (lets say 24 rows and 30 columns) it gives error.

    Your macro mehmetcik gives error when i have decimals in the column which we are sorting by (price column is the 2nd column and the column that we choose to sort). When i round all numbers and execute it works perfectly, but the decimals are very important in my case.

    Thanks in advance for any further help you may provide

+ 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. separate the last digit, and then sort according to the 10 pairs in a row
    By jorel in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-04-2020, 03:51 PM
  2. Replies: 7
    Last Post: 10-17-2019, 12:00 PM
  3. Find Duplicates across 2 pairs of columns (2 columns against 2 columns)
    By erjfly2013 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-09-2015, 06:52 PM
  4. Replies: 6
    Last Post: 05-30-2013, 04:58 PM
  5. [SOLVED] Using VBA / Macro to autosort pairs of connecting columns and rows?
    By Stephen2135 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-18-2013, 09:04 PM
  6. Sort Largest to Smallest For Column Pairs
    By bluestarcloudx in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-25-2012, 08:19 PM
  7. Sort on rows (not columns)
    By po2206 in forum Excel General
    Replies: 2
    Last Post: 03-16-2006, 03:55 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