Closed Thread
Results 1 to 29 of 29

Find All Combinations That Equal A Given Sum

  1. #1
    Forum Contributor
    Join Date
    05-26-2016
    Location
    India
    MS-Off Ver
    Microsoft 2021
    Posts
    324

    Find All Combinations That Equal A Given Sum

    Hello
    I want to know how can I find all possible combinations from given list of numbers.
    A Image is attached for reference
    Any formula or VBA code
    Attached excel file can find only one possible combination
    I want multiple solutions/combinations as image attached in excel file.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,178

    Re: Find All Combinations That Equal A Given Sum

    See attached.
    Attached Files Attached Files

  3. #3
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,943

    Re: Find All Combinations That Equal A Given Sum

    Here is a file with Harlan Grove's code - press the button and select the ranges when prompted. Note that if you have upwards of 20 or so values, the code can take a loooooooooong time to run.
    Attached Files Attached Files
    Bernie Deitrick
    Excel MVP 2000-2010

  4. #4
    Forum Contributor
    Join Date
    05-26-2016
    Location
    India
    MS-Off Ver
    Microsoft 2021
    Posts
    324

    Re: Find All Combinations That Equal A Given Sum

    Excellent Support and solution
    Thank you

  5. #5
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: Find All Combinations That Equal A Given Sum

    While this can be done, its something that is asked alot. Excel is really the wrong tool for this job.

    In a small sample set like you have provided, the number of combinations to check is relatively low. If you data set is much larger you may be in for a surprise.

    Mathematically, in the sample you provided with 8 numbers, your possible combinations to check is something like 8^8 combinations if I am not mistaken (I am sleepy could be wrong). In a larger data set it could get exponentially larger. This basically means it can take a really, really really long time to complete and can easily surpass the amount of space a single column and/or sheet provides for results. A while back I had a customer as for the same thing for ~30 values (may have been some criteria dont recall specifics). Our code, as optimized as possible, was running 10 hours later with no end in sight. The problem is without constraints it has to check all possible combinations to exhaust the correct results.

    One of the issues is VBA is single threaded, so it can only do one check at a time instead of trying x combinations simultaneously.

    Its possible to speed things up drastically if you put constraints on what to return, like the first x number of combinations. Something like that can drastically reduce the time and effort to cycle through.
    Ways to get help: Post clear questions, explain your overall goal, supply as much background as possible, respond to questions asked of you by those trying to help and post sample(s) files.

    "I am here to help, not do it for people" -Me

  6. #6
    Forum Contributor
    Join Date
    05-26-2016
    Location
    India
    MS-Off Ver
    Microsoft 2021
    Posts
    324

    Re: Find All Combinations That Equal A Given Sum

    Good solution too thanks for quick response
    thank you all for solutions

  7. #7
    Forum Contributor
    Join Date
    05-26-2016
    Location
    India
    MS-Off Ver
    Microsoft 2021
    Posts
    324

    Re: Find All Combinations That Equal A Given Sum

    I am going to increase the range to almost 200 numbers and i will share my experience with you soon

  8. #8
    Forum Contributor
    Join Date
    05-26-2016
    Location
    India
    MS-Off Ver
    Microsoft 2021
    Posts
    324

    Re: Find All Combinations That Equal A Given Sum

    Quote Originally Posted by JohnTopley View Post
    See attached.
    What if I increase the range to 200 numbers or 300 numbers will this work fine ?

  9. #9
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,943

    Re: Find All Combinations That Equal A Given Sum

    I'm not sure if the we will still be alive by the time Excel finishes with 200 possible numbers..... If you limited the count of numbers used to make the total to a small number, then maybe....

  10. #10
    Forum Contributor
    Join Date
    05-26-2016
    Location
    India
    MS-Off Ver
    Microsoft 2021
    Posts
    324

    Re: Find All Combinations That Equal A Given Sum

    my real problem is that I have 100 numbers which have different values like 1 436 , 2 540 .. etc etc
    i want to find all possible combination for a specific number from all given numbers.

  11. #11
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,943

    Re: Find All Combinations That Equal A Given Sum

    If you are looking to add 2 or 3 or 4 numbers - like matching invoices totals to items purchased, then you would have a chance. If you are looking to add up to 100 numbers to reach your total, then you are out of luck.

  12. #12
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,178

    Re: Find All Combinations That Equal A Given Sum

    As already noted you will be waiting for ever with the solution I posted!!!

    I have only tried it with < 20 numbers of limited range.

  13. #13
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: Find All Combinations That Equal A Given Sum

    Context would likely help us help you. Knowing why you want to do this may present the chance to use an alternative method or incorporate something into the provided solution to make it practical. So why do you need to do this? What are you going to do with all those combinations? Is there something specific in those combinations you are looking for?

    Reality is, the basic question of "Finding all combinations that equal a value" is very easy to comprehend and ask but mathematically very taxing to do. Excel simply wasnt made for that type of thing. As I recall from my example we ended up convincing the customer to do it in Access as some feature it it was able to do this within seconds/minutes instead of hours/days/never. I dont mess with Access so I couldnt say how to do it there.

    And again, adding some constraints could drastically speed things up like stopping after x matches for example. Context would shed light on if that feasible or not.

    With 200 numbers, you are looking at 200^200 possible combinations or 1.6069380442589902755419620923412e+460 combinations according to Win calc if it can even handle numbers as large as it would be. Excel would have to try each of those combinations 1 at a time, check if it equals your target, store it if it does or know to skip it if it doesnt and then try the next. Its as tedious as it sounds.

  14. #14
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,943

    Re: Find All Combinations That Equal A Given Sum

    It's not as bad as all that - it is really only on order of 200! and not 200^200, reduced by other constraints like order not being important and stopping when the sum exceeds the target value. Still long enough that the heat death of the universe will probably occur before it is done

  15. #15
    Forum Contributor
    Join Date
    05-26-2016
    Location
    India
    MS-Off Ver
    Microsoft 2021
    Posts
    324

    Re: Find All Combinations That Equal A Given Sum

    ok I would like to clear the motive for this question
    First of all its not my homework . Its totally for religious purpose or social . According to my religion every name have specific some of digits e.g 672 is the digit for my name. we have 100 unique names for it and every name have different value . once we find the value of our name then we look the combination for ou name from those 100 names/value .. then we choose best one for our luck ... you can say astrology type thing it is.
    I wish it would not go for long sets like {v1,v2,v3,v4,v5,v6,v7,v8} = SUM.... sets or combinations should be small

    Quote Originally Posted by Zer0Cool View Post
    Context would likely help us help you. Knowing why you want to do this may present the chance to use an alternative method or incorporate something into the provided solution to make it practical. So why do you need to do this? What are you going to do with all those combinations? Is there something specific in those combinations you are looking for?

    Reality is, the basic question of "Finding all combinations that equal a value" is very easy to comprehend and ask but mathematically very taxing to do. Excel simply wasnt made for that type of thing. As I recall from my example we ended up convincing the customer to do it in Access as some feature it it was able to do this within seconds/minutes instead of hours/days/never. I dont mess with Access so I couldnt say how to do it there.

    And again, adding some constraints could drastically speed things up like stopping after x matches for example. Context would shed light on if that feasible or not.

    With 200 numbers, you are looking at 200^200 possible combinations or 1.6069380442589902755419620923412e+460 combinations according to Win calc if it can even handle numbers as large as it would be. Excel would have to try each of those combinations 1 at a time, check if it equals your target, store it if it does or know to skip it if it doesnt and then try the next. Its as tedious as it sounds.

  16. #16
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,178

    Re: Find All Combinations That Equal A Given Sum

    Don't quote whole posts -- it's just clutter. If you are responding to a post out of sequence, limit quoted content to a few relevant lines that makes clear to whom and what you are responding

    For normal conversational replies, try using the QUICK REPLY box below.

  17. #17
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,943

    Re: Find All Combinations That Equal A Given Sum

    In the attached, put your list of names in column A starting in A3, and their number values in B starting in B3. Put your target value in C2 and click the button. And be patient. Start with a smaller set of your actual names to get a feel for how long it might take. This will find the combinations of 3 ,4, or 5. I ignored 2 but there may be some solutions for combinations of two - you could use formulas to figure those out easily, though.
    Attached Files Attached Files
    Last edited by Bernie Deitrick; 01-28-2018 at 11:04 AM.

  18. #18
    Registered User
    Join Date
    07-27-2018
    Location
    Dublin, Ireland
    MS-Off Ver
    2013
    Posts
    14

    Re: Find All Combinations That Equal A Given Sum

    Hello!
    I've stumbled upon this response and the attached Macro and find it really helpful.
    I've been looking for something like this from Credit Control perspective whereby we receive a payment for several invoices relating to several different accounts.
    Is there a way of adding a column for account numbers relating to the values and when the results are produced on the "find sum solutions" sheet that it also give s the account numbers relating to the transaction values?
    I can definitely just live with this solution but just checking if there is anything out there more fine tuned for this particular purpose?
    Awesome solution anyway thanks!!

  19. #19
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,719

    Re: Find All Combinations That Equal A Given Sum

    Welcome to the forum!

    Unfortunately your post does not comply with Rule 2 of our Forum RULES. Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too.
    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.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  20. #20
    Registered User
    Join Date
    10-22-2018
    Location
    New Jersey
    MS-Off Ver
    2011
    Posts
    7

    Re: Find All Combinations That Equal A Given Sum

    Quote Originally Posted by Bernie Deitrick View Post
    In the attached, put your list of names in column A starting in A3, and their number values in B starting in B3. Put your target value in C2 and click the button. And be patient. Start with a smaller set of your actual names to get a feel for how long it might take. This will find the combinations of 3 ,4, or 5. I ignored 2 but there may be some solutions for combinations of two - you could use formulas to figure those out easily, though.
    Hi there. I downloaded your file Name combos.xlsm‎ and tried to rework it using my values but it wants me to debug the code or abort. I don't see an option to upload my file but the changes I made were adding 4 Names and values, changing the target number to 60000 and changing all number values starting with 5500 with a range up to 17000. When I click debug it takes me to this line in the code - iTarget = Cells(2, "C").Value Any chance you can advise me on how to get my figures to work in your model?

  21. #21
    Registered User
    Join Date
    10-22-2018
    Location
    New Jersey
    MS-Off Ver
    2011
    Posts
    7

    Re: Find All Combinations That Equal A Given Sum

    Quote Originally Posted by cutty382 View Post
    Hi there. I downloaded your file Name combos.xlsm‎ and tried to rework it using my values but it wants me to debug the code or abort. I don't see an option to upload my file but the changes I made were adding 4 Names and values, changing the target number to 60000 and changing all number values starting with 5500 with a range up to 17000. When I click debug it takes me to this line in the code - iTarget = Cells(2, "C").Value Any chance you can advise me on how to get my figures to work in your model?
    I found the answer. I had to change your code from INTEGER to LONG and it ran.

  22. #22
    Registered User
    Join Date
    11-11-2018
    Location
    Las Vegas
    MS-Off Ver
    2013
    Posts
    2

    Re: Find All Combinations That Equal A Given Sum

    Thank you so much!! but explanation maybe be helpful as well!!

  23. #23
    Registered User
    Join Date
    12-22-2019
    Location
    UK
    MS-Off Ver
    2016
    Posts
    6

    Re: Find All Combinations That Equal A Given Sum

    I have 20 small numbers for which I want to find all of the combinations of 8 of the numbers that sum to 26, is this possible?

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

    Re: Find All Combinations That Equal A Given Sum

    Administrative Note:

    Welcome to the forum.

    We are happy to help, however whilst you feel your request is similar to this thread, experience has shown that things soon get confusing when answers refer to particular cells/ranges/sheets which are unique to your post and not relevant to the original.

    Please see Forum Rule #4 about hijacking and start a new thread for your query.

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread

  25. #25
    Registered User
    Join Date
    02-02-2020
    Location
    Up india
    MS-Off Ver
    Version 16
    Posts
    1

    Re: Find All Combinations That Equal A Given Sum

    Dear Sir,
    Please provide excel sheet for cricket fantasy game Dream11 team for all possible combinations

  26. #26
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: Find All Combinations That Equal A Given Sum

    Quote Originally Posted by Zer0Cool View Post
    . . . Mathematically, in the sample you provided with 8 numbers, your possible combinations to check is something like 8^8 combinations if I am not mistaken . . .
    Given N numbers viewed as a set, there are 2^N subsets including the empty set and the entire set. Since the empty set need not be checked, there are 2^N-1 subsets to sum, though the 1-element subsets and the entire set are straightforward. You only need to come up with sums of subsets with up to ROUNDUP(N/2,0) elements because sums of subsets with more elements would be given by SUM(entire_set) - SUM(subsets w/up to ROUNDUP(N/2,0) elements). Which means you're trying to match sums of subsets with up to ROUNDUP(N/2,0) elements to target_sum or to SUM(entire_set) - target_sum.

  27. #27
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2402
    Posts
    13,446

    Re: Find All Combinations That Equal A Given Sum

    @ Ramsrahi
    Administrative Note:

    Welcome to the forum.

    We are happy to help, however whilst you feel your request is similar to this thread, experience has shown that things soon get confusing when answers refer to particular cells/ranges/sheets which are unique to your post and not relevant to the original.

    Please see Forum Rule #4 about hijacking and start a new thread for your query.

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread

  28. #28
    Registered User
    Join Date
    03-29-2020
    Location
    Florida, US
    MS-Off Ver
    Office 2016
    Posts
    1

    Re: Find All Combinations That Equal A Given Sum

    Quote Originally Posted by Bernie Deitrick View Post
    In the attached, put your list of names in column A starting in A3, and their number values in B starting in B3. Put your target value in C2 and click the button. And be patient. Start with a smaller set of your actual names to get a feel for how long it might take. This will find the combinations of 3 ,4, or 5. I ignored 2 but there may be some solutions for combinations of two - you could use formulas to figure those out easily, though.
    Hello bernie, thanks, just what I was looking for. Know this is an old post but any chance I could use the same for a combination of 6 and 7s? No idea how to do this. But possible you could upload a similar excel file with those combinations? ( 6 and 7 that is). Would really appreciate it. Thanks in advance.

  29. #29
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,719

    Re: Find All Combinations That Equal A Given Sum

    Administrative Note:

    Welcome to the forum.

    We are happy to help, however whilst you feel your request is similar to this thread, experience has shown that things soon get confusing when answers refer to particular cells/ranges/sheets which are unique to your post and not relevant to the original.

    Please see Forum Rule #4 about hijacking and start a new thread for your query.

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread

Closed Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. How to calculate combinations from set of data to equal sum
    By Rheanna in forum Excel Formulas & Functions
    Replies: 20
    Last Post: 06-16-2015, 01:18 PM
  2. [SOLVED] Solve for integer combinations that equal a given ratio
    By rinkjames in forum Excel General
    Replies: 4
    Last Post: 11-17-2012, 08:55 AM
  3. Find Possible Sum Combinations That Equal Given Value
    By mdsyam in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-11-2012, 04:21 AM
  4. Combinations of numbers to equal one total
    By Sunnangel in forum Excel General
    Replies: 2
    Last Post: 04-09-2009, 12:28 PM
  5. Number combinations to equal total
    By unitedfasteners in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-24-2008, 12:00 AM
  6. [SOLVED] Macro to add numbers to create combinations that equal certain amo
    By Lauren qt314 in forum Excel General
    Replies: 3
    Last Post: 04-21-2005, 01:06 PM
  7. Find combinations of numbers to equal a value
    By jubu in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-18-2005, 09:06 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