+ Reply to Thread
Results 1 to 22 of 22

Complex Combinatorial sort VBA/Macro

  1. #1
    Registered User
    Join Date
    09-04-2017
    Location
    Prague
    MS-Off Ver
    2016
    Posts
    47

    Complex Combinatorial sort VBA/Macro

    Hi everyone,

    I have a tricky task Let me clarify that step by step below.

    Input
    • table with 5 columns
    • columns are: combination, place1, place2, place3, income
    • column combination represents a number of each combination (combination of 3 places)
    • place1, 2, 3 are basically points where a car needs to go
    • income says how much we get paid to drive in a specific combination (way)

    e.g. we can say that combination #6 brings 13628.40 income and the car needs to go through places A, O & M.

    First of all, I need to find out the way how I specify: Let's say that formula will go from combination 1 to 197. We currently know that all combinations are sort from the biggest price to the lowest one.

    Secondly, I need to set up the rule saying, once you drove through a specific place, DO NOT drive through that point again.
    e.g. We take the combination #1 F, A, O and we know that cannot drive again to those points. So a formula looks for other combinations the next possible combination is #13 since M, K, N do not match with previous combination F, A, O. Now we have already 6 places where the car cannot drive anymore. F, A, O, M, K, N. Then the next available combination is E, I, J. And it goes like that until it does not have all possible combinations found.

    We can say that all found combinations are below:

    combination place1 place2 place3 income
    1 F A O 16566
    13 M K N 12126
    19 E I J 10610
    74 Q T R 5500
    93 G H C 4695


    TOTAL Income is 49549. This result is based on the fact, that formula goes from combination #1 to #197 where biggest incomes are first. What if the table is sort in a different way? How many combinations can be there? 197 factorial? How can find the best distribution in order to get the highest income value?
    Attached Files Attached Files

  2. #2
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Complex Combinatorial sort VBA/Macro

    Maybe :
    Please Login or Register  to view this content.
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  3. #3
    Registered User
    Join Date
    09-04-2017
    Location
    Prague
    MS-Off Ver
    2016
    Posts
    47

    Re: Complex Combinatorial sort VBA/Macro

    PERFECT! It solves the first part of the problem, thank you so much.

    The other question is still: Is there another way how to find the biggest total income? Or is total income 49549 really the biggest possible one?
    I would love to have a macro which will be able to sort all combinations (from 1 to 197) in 197! (factorial) ways. It will record all sums of total income. Simultaneously, it should also assign how the combinations were sort for each total income. It means that at the end of the day I will be able to say: Ok, no I know that total income 49549 is the biggest possible total income where following combinations numbers exist: 1, 13, 19, 74, 93.

    Thank you, I appreciate any idea.
    PS: I guess it would take Excel forever to calculate such a formula but I hope there is some solution

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Complex Combinatorial sort VBA/Macro

    I would love to have a macro which will be able to sort all combinations (from 1 to 197) in 197! (factorial) ways.
    1. If the universe were a simple cube 40 billion light years on a side, and a quark was a cube 10^-18 m on a side, how many quarks would it take to pack the universe solid?

    2. How does that number compare in size to 197!
    Last edited by shg; 03-15-2018 at 11:45 AM.
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Registered User
    Join Date
    09-04-2017
    Location
    Prague
    MS-Off Ver
    2016
    Posts
    47

    Re: Complex Combinatorial sort VBA/Macro

    I agree with you I just do not know how to assess this problem I do not say that is the way how to do that.

  6. #6
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Complex Combinatorial sort VBA/Macro

    Don't lose the hope yet, you have so many constraints, that there must be no duplicate item on columns C-D-E, this will reduce the combinations a lot.
    Running this code for 1 minute will produce all the 184556 combinations that meet your criteria (no dupes on C-D-E) :
    Please Login or Register  to view this content.
    Last edited by karedog; 03-15-2018 at 12:33 PM. Reason: incomplete code

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

    Re: Complex Combinatorial sort VBA/Macro

    Try this .It works. This code gives Value 1 to all rows In G column with highest sum after trying all combinations with your conditions.
    code
    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.

  8. #8
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2503 (Windows 11 Home 24H2 64-bit)
    Posts
    90,051

    Re: Complex Combinatorial sort VBA/Macro

    Rule 08: Cross-posting Without Telling Us

    Your post does not comply with Rule 8 of our Forum RULES. Do not cross-post your question on multiple forums without telling us about your threads on other forums.

    Post a link to any other forums where you have asked the same question. If you have fewer than 10 posts here, you will not be able to post a link, but you must still tell us where else you have asked the question.

    Cross-posting is when you post the same question in other forums on the web. The last thing you want to do is waste people's time working on an issue you have already resolved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser) to the cross-post.

    Expect cross-posted questions without a link to be closed and a message will be posted by the moderator explaining why. We are here to help so help us to help you!

    Read this to understand why we ask you to do this, and then please edit your first post to include links to any and all cross-posts in any other forums (not just this site).

    No further help to be offered, please, until the OP has complied with this request.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  9. #9
    Registered User
    Join Date
    09-04-2017
    Location
    Prague
    MS-Off Ver
    2016
    Posts
    47

    Re: Complex Combinatorial sort VBA/Macro

    Thank you so much! Both approaches work perfectly how I want. Karedog's code is better for the overall overview and takes let than a minute. Much appreciated guys, you helped me a lot

  10. #10
    Registered User
    Join Date
    09-04-2017
    Location
    Prague
    MS-Off Ver
    2016
    Posts
    47

    Re: Complex Combinatorial sort VBA/Macro

    Quote Originally Posted by AliGW View Post
    Rule 08: Cross-posting Without Telling Us

    Your post does not comply with Rule 8 of our Forum RULES. Do not cross-post your question on multiple forums without telling us about your threads on other forums.

    Post a link to any other forums where you have asked the same question. If you have fewer than 10 posts here, you will not be able to post a link, but you must still tell us where else you have asked the question.

    Cross-posting is when you post the same question in other forums on the web. The last thing you want to do is waste people's time working on an issue you have already resolved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser) to the cross-post.

    Expect cross-posted questions without a link to be closed and a message will be posted by the moderator explaining why. We are here to help so help us to help you!

    Read this to understand why we ask you to do this, and then please edit your first post to include links to any and all cross-posts in any other forums (not just this site).

    No further help to be offered, please, until the OP has complied with this request.
    Thank you for sharing that with me. I am sorry, I did not realize it. Here is the link: https://www.mrexcel.com/forum/excel-...vba-macro.html

  11. #11
    Registered User
    Join Date
    09-04-2017
    Location
    Prague
    MS-Off Ver
    2016
    Posts
    47

    Re: Complex Combinatorial sort VBA/Macro

    Hello karedog,

    Thank you again for your valuable help, I do appreciate it!

    I have another challenging question related to this topic.
    Please have a look on the attached file where I try to demonstrate what should be the ideal outcome
    https://drive.google.com/file/d/1KA4...ew?usp=sharing

    2018-03-17_23-53-46.png

    The thing is that I would like to extend the outcome. I have 346 more combinations where are only 2 places to visit for each combination.
    I would love to find all possible combinations between generated 184556 combinations and new 346 combinations based on the same restrictions as for the previous calculation. It means if there is no duplication in places both combinations can be merged to one.
    Thank you so much for your help.

  12. #12
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Complex Combinatorial sort VBA/Macro

    You are out of luck this time

    Just trying a very small portion of data (a small slice of both arrays), it's already run out of system resources (result is much more than Excel rows, out of memory, take "forever" time, etc).

    The code below just to show that this code works, but it will never complete if you do the whole data.

    The code only process 2 rows of left array :
    a = .Range("B3:D4").Value
    while it should be (if you want to process the whole left array) :
    a = .Range("B3:D" & .Cells(.Rows.Count, "B").End(xlUp).Row).Value

    and it only process 105 rows of right array :
    a = .Range("H3:K107").Value
    while it should be (if you want to process the whole right array) :
    a = .Range("H3:K" & .Cells(.Rows.Count, "H").End(xlUp).Row).Value

    Just doing these slices, it already took 2 minutes and produce 622788 rows result

    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    09-04-2017
    Location
    Prague
    MS-Off Ver
    2016
    Posts
    47

    Re: Complex Combinatorial sort VBA/Macro

    Hello karedog,

    I see! Anyway, it helps me a lot, I will try to set up some rules to figure it out. Thank you.

    I have been trying to solve a similar problem. There is a worksheet with combinations where there are only two places to visit. It's basically the same problem as the initial one but for now, I have only 2 places to visit per combination. Could you please help to find out how to generate all combinations based on the same rules as previously? It means basically that one place cannot be visited twice? Please find the attached Workbook.
    Attached Files Attached Files

  14. #14
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Complex Combinatorial sort VBA/Macro

    Again, this will be the same problem as post #12 (I highlight the lines where the code working with slice of array vs whole array), but here is the code :
    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    09-04-2017
    Location
    Prague
    MS-Off Ver
    2016
    Posts
    47

    Re: Complex Combinatorial sort VBA/Macro

    Awesome, thank you so much! Eventually and hopefully, the final question regarding combinations? Is it possible to have a similar code for an example below? First generated combinations are in columns B,C,D and other secondly generated combinations are in columns H,I,J. Theirs merged combination are sum up in columns M,N,O. I am looking for the results in columns M,N,O. I put together two examples how it should look like. Unluckily, I was trying so many hours to create a proper VBA code without a useful solution Could you please advise?
    file here: https://goo.gl/tVKVSj

    combinations.png

  16. #16
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Complex Combinatorial sort VBA/Macro

    Maybe :
    Please Login or Register  to view this content.

  17. #17
    Registered User
    Join Date
    09-04-2017
    Location
    Prague
    MS-Off Ver
    2016
    Posts
    47

    Re: Complex Combinatorial sort VBA/Macro

    That works perfectly! I have been working on reducing extra calculation in the code by setting up the rule where code checks whether is total sum of income higher than certain value (e.g. 45000). If yes, then it skips all the formulas and saves total calculation time otherwise if there is more rows in the sheet it takes a long time to get results. I cannot find the proper way how to set it up. Do you think there could be a way how to check first how much is total sum of two selected combinations is over the selected limit and if not, then continue to compare another pair of combinations (rows). Thank you a lot for your all help!

  18. #18
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Complex Combinatorial sort VBA/Macro

    Modified code on Post #6 :
    Please Login or Register  to view this content.

    Modified code on Post #12 :
    Please Login or Register  to view this content.

    Modified code on Post #14 :
    Please Login or Register  to view this content.

    Modified code on Post #16 :
    Please Login or Register  to view this content.

  19. #19
    Registered User
    Join Date
    09-04-2017
    Location
    Prague
    MS-Off Ver
    2016
    Posts
    47
    Hello,

    Thank you for another solution. Doesn't it only generate first 25000 results? I was thinking to consider all possible results but only those results with total income higher than 45000 will be printed.

    Thank you for your help.

  20. #20
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Complex Combinatorial sort VBA/Macro

    No, it is cut a branch of combinations as soon as combining one item with other items after it, has reach total income 25000 (assuming the data are sorted descending from big to small), this way you can reduce a lot of unnecessary combinations.

    If you still want to keep calculating all combinations, but only don't display (put the result on Excel sheet) for value less then 45000, the time won't changed too much. The slowness is come from iterating a very much of combinations (as you said : factorial), not from displaying to screen.

  21. #21
    Registered User
    Join Date
    09-04-2017
    Location
    Prague
    MS-Off Ver
    2016
    Posts
    47

    Re: Complex Combinatorial sort VBA/Macro

    Alright, I understand. I still want to keep calculating all combinations even thought the time will not change. Anyway, I need to display only those with total income higher than 25000. Since there is more results than the maximum amount of rows in Excel. Thank you.

  22. #22
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Complex Combinatorial sort VBA/Macro

    In that case, you need to wrap two lines at the end of sub, with an IF..ENDIF block (shown with blue lines).
    I'll show you for the code on post #6, you can do the rest by yourself.

    Please Login or Register  to view this content.
    Note : although you now have enough rows to display the result, it is not guaranteed that there is no other error such as memory (RAM) is enough to finish the job.

+ 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. Sort complex Excel Table with VBA
    By mthwi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-25-2017, 04:33 PM
  2. [SOLVED] help with a complex sort of adding function
    By disterbed in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 12-18-2014, 10:37 PM
  3. Excel to sort complex numbers in correct order
    By RichTea88 in forum Excel General
    Replies: 5
    Last Post: 12-04-2014, 08:18 AM
  4. Help with solver - reverse combinatorial problem / transportation optimization scenario
    By jon_nxt2000 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-30-2014, 07:25 PM
  5. Complex sort returning duplicates (no really)
    By gotheframe in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 05-02-2014, 11:41 AM
  6. Replies: 7
    Last Post: 06-15-2010, 08:17 PM
  7. Complex Sort Question
    By Chas in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-15-2006, 10:20 PM

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