+ Reply to Thread
Results 1 to 16 of 16

How to find combination of numbers to match balance?

  1. #1
    Registered User
    Join Date
    08-05-2021
    Location
    London, England
    MS-Off Ver
    V10
    Posts
    4

    How to find combination of numbers to match balance?

    I have 45 input cash amounts, I need to find out if any combination of these sums to a balance

    It could be just two input numbers, or it could be 35 input numbers

    Example:
    My input numbers are...
    -1,860
    1,860
    48
    1,300
    90
    50,000
    -23,624
    24,779
    -24,779
    -3,722
    -3,338
    -880
    -152
    1,561
    2,404
    3,338
    3,722
    4,957
    -305,718
    -41,307
    -41,307
    -30,980
    -10,326
    -6,196
    -2,915
    -2,571
    -2,065
    -87
    -52
    -17
    17
    52
    87
    2,065
    2,571
    6,196
    10,326
    30,980
    41,307
    41,307
    100,503
    110,168
    305,718
    -144
    144


    I need to find out if some of these can be summed to give a Balance of -209,000



    I have many hundreds of these to check, with variable numbers of inputs


    I can't see how to write a macro do check these


    Any ideas?

    Thanks in advance for any suggestions

  2. #2
    Spammer
    Join Date
    10-23-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2003, Office 365
    Posts
    1,237

    Re: How to find combination of numbers to match balance?

    You could use Solver to do this.

    Put your numbers in A1:A45
    In C1 put in =A1*B1 and drag down to C45, Make C46 =SUM(C1:C45)
    Then use solver for cell C46 = -209000 with the constraint that B1:B45 is binary

    Not my idea. I saw it somewhere a while ago.

    (see attached)
    Attached Files Attached Files

  3. #3
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: How to find combination of numbers to match balance?

    There is a problem with using Solver for this kind of problem as Solver only will find one solution where there could (probably) be quite a number of these.

    Tushar-Menta did set up a vba solution and a forum member JohnTopely added a more user friendly interface. I've uploaded a sample file where I set result to 10 different
    combinations and there are many more solutions.

    On his Excel page there are an add-in that finds combinations that add up to a specific number but it's not a freebie. Link
    http://www.tushar-mehta.com/excel/so...rget/index.htm

    Alf
    Attached Files Attached Files

  4. #4
    Spammer
    Join Date
    10-23-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2003, Office 365
    Posts
    1,237

    Re: How to find combination of numbers to match balance?

    Quote Originally Posted by Alf View Post
    There is a problem with using Solver for this kind of problem as Solver only will find one solution where there could (probably) be quite a number of these.
    Alf
    How do you know it's a "problem"? The OP may only require one solution. He certainly hasn't indicated otherwise.

  5. #5
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: How to find combination of numbers to match balance?

    The OP may only require one solution.
    Do we know if the OP is aware of the multiple solution that can be found? And using a "tool" should one not be aware of its limitations when using it?

    I have many hundreds of these to check,
    The Excel solver is limited to max 200 constrains so if more than 200 rows is to be tested I would recommend the "OpenSolver" a freebie made by the University of Auckland NZ. Running solver in a linear mode there are no limits to the number of constraints. OpenSolver integrates nicely with Excel and model can be build using Excel solver and then run by OpenSolver

    Link:

    https://opensolver.org/installing-opensolver/

    Alf
    Last edited by Alf; 08-06-2021 at 06:27 AM.

  6. #6
    Spammer
    Join Date
    10-23-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2003, Office 365
    Posts
    1,237

    Re: How to find combination of numbers to match balance?

    Do we know if the OP is aware of the multiple solution that can be found?
    What...do you think that he might believe that there is only one unique solution for every set of numbers?

    Here is a thought.

    Here are 4 numbers, 4,6,20,10. Do you think he might believe there is only one solution to make a total of 30?

  7. #7
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,732

    Re: How to find combination of numbers to match balance?

    In the examples given by the OP there are several pairs of numbers which are +ve and -ve, and thus cancel each other out in any sum. So, if there is a solution to finding a Balance which adds to -209.00, then there are bound to be other solutions which include these pairs in different combinations.

    We need more information from the OP.

    Pete

  8. #8
    Registered User
    Join Date
    08-05-2021
    Location
    London, England
    MS-Off Ver
    V10
    Posts
    4

    Re: How to find combination of numbers to match balance?

    I just need to know if any solution exists, I don't care if there are many possible solutions

    Also, each number can only be used once. Solver comes up with solutions like 2800 * "first number" - 6389 * "second number"...etc

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

    Re: How to find combination of numbers to match balance?

    The OP may not appreciate just how many solutions there might be.

    The smallest sum possible is the sum of the negative values, and the largest sum possible is the sum of the positive values. Considering all are whole numbers, the number of different sums possible is ...

    largest - smallest + 1

    With n numbers, there are 2^n ways to combine.

    Dividing the latter by the former gives the AVERAGE number of ways to arrive at ANY given sum, which is about 30 million:

    B
    C
    D
    1
    smallest sum
    -502,040
    C1: =SUMIF($A$1:$A$45, "<0")
    2
    largest sum
    745,500
    C2: =SUMIF($A$1:$A$45, ">0")
    3
    number of possible sums
    1,247,541
    C3: =C2 - C1 + 1
    4
    5
    numbers
    45
    C5: =COUNT(A1:A45)
    6
    ways to combine
    35,184,372,088,832
    C6: =2^C5
    7
    average multiplicity of solutions
    28,202,979
    C7: =C6/C3
    Last edited by shg; 08-06-2021 at 03:31 PM.
    Entia non sunt multiplicanda sine necessitate

  10. #10
    Spammer
    Join Date
    10-23-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2003, Office 365
    Posts
    1,237

    Re: How to find combination of numbers to match balance?

    Quote Originally Posted by Ted888888 View Post
    I just need to know if any solution exists, I don't care if there are many possible solutions

    Also, each number can only be used once. Solver comes up with solutions like 2800 * "first number" - 6389 * "second number"...etc
    That is precisely why I said "...with the constraint that B1:B45 is binary". Did you download my attachment in post #2?

  11. #11
    Spammer
    Join Date
    10-23-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2003, Office 365
    Posts
    1,237

    Re: How to find combination of numbers to match balance?

    The OP may not appreciate just how many solutions there might be.
    ...or even care

    I don't care if there are many possible solutions
    Last edited by Croweater; 08-06-2021 at 07:33 PM.

  12. #12
    Registered User
    Join Date
    08-05-2021
    Location
    London, England
    MS-Off Ver
    V10
    Posts
    4

    Re: How to find combination of numbers to match balance?

    Thank you! Yes, I've tried your Solver solution, that does not seem to work as I can't specify the constraint that B1:B45 is binary, perhaps its my lack of knowledge?

    The addin TM Match works though, it finds a solution in under a second

  13. #13
    Registered User
    Join Date
    08-05-2021
    Location
    London, England
    MS-Off Ver
    V10
    Posts
    4

    Re: How to find combination of numbers to match balance?

    Yes, the number of combinations is huge!

    This addin solved it in under a second though! TM Match Target - the forum won't allow me to post the link

  14. #14
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: How to find combination of numbers to match balance?

    See solver setting i.e. click button marked "Options". Check if box marked "Ignore Integer Constraint" is ticked. If so untick it and test again.

    Alf

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

    Re: How to find combination of numbers to match balance?

    The OP may not appreciate just how many solutions there might be.
    ...or even care
    I don't assume your incurious nature extends to include the OP.

  16. #16
    Spammer
    Join Date
    10-23-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2003, Office 365
    Posts
    1,237

    Re: How to find combination of numbers to match balance?

    I don't assume your incurious nature extends to include the OP.
    Well maybe you should, after all it was him that said it, not me.

    (The first line of post #8 is a dead give-away).
    Last edited by Croweater; 08-07-2021 at 07:58 PM.

+ 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. Combination of SUMPRODUCT and INDEX-MATCH, to sum all positive numbers.
    By PaolosKiE in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 08-25-2020, 02:04 AM
  2. Formula to find closest combination of numbers ...
    By 951Michael in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 11-21-2019, 04:09 PM
  3. [SOLVED] Find combination of numbers that when added, equal a known value.
    By mgblair in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-09-2015, 08:45 PM
  4. Match Find formula request, sheet balance switch
    By annazet in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 06-20-2014, 06:14 AM
  5. Need Formule to find best combination of numbers...
    By Shreyalmuri in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 01-09-2013, 07:46 AM
  6. how to i find combination 6 of 8 numbers in lotto 6/45
    By bemax38 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-02-2012, 04:47 AM
  7. Replies: 5
    Last Post: 07-01-2008, 09:36 AM

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