'Excel Function to find a combination of numbers

I have a number on a spreadsheet which is a combination of many different numbers on a list.

For example:

A list contains: 100, 200, 250, 500, and 1000
The number I need to explain is: 800

The answer would be 500, 200, 100.

I'm dealing with over 1500 currency cells ($xxxx.xx) which make a total (and not all are used, so SUM is useless). I need to understand which numbers were used to create that total (Which isn't a formula, it's a hard-coded number).

THE QUESTION:
Is there a function or VBA that will systematically combine numbers in a given range until it determines which numbers can be added together to make the total?

I want to know before I start writing a brute-force algorithm.



Solution 1:[1]

Okay, figured out the solution that worked best for me:

First, I found some vba that lets you create infinite strings of binary (rather than the 9 bits Excel has built in). I then used this code to create a UDF for this purpose... Since I was working with chunks of 20-40 "bits", this was absolutely necessary.

Second, I made a counter loop which increased by 1, then changed the binary string to reflect the new number. (1,10,11,100,101,110,111, etc.)

Third, I wrote a formula that breaks the binary string apart, and assigns each 1 or 0 to the corresponding number in the cell next to it. (Just using the LEN(),RIGHT(), and MID() functions to recognize 1s and 0s).

Fourth, I multiplied each value by the 1 or 0 next to it, and then compared the sum of all multiplied numbers to the target value I was looking for.

100% of the time, if given clean data, this finds a solution, if one exists. (Mostly time is a factor though, since this is an exponential function, so the more bits you have, the longer it takes to cycle through them)

This ran at about 3 million combinations in 4 minutes, give or take depending on a few factors

I redid the worksheet, and doubled the speed by having 5 columns, each incremented by one, and having the counter increment by 5 (rather than 1).

Solution 2:[2]

You can use SOLVER in excel to get the result.

You can activate it in ADD-INS and it should show up in DATA tab.

You set up your spreadsheet like this:

In one column You have list of numbers You want to check Next column is all zeroes (0) Third column is First*Second (for example 100 * 0) so in the beginning its zero for all rows

Than You add summary of third column and it should also be zero. Example how this data can look like:

100 0   0
200 0   0
500 0   0
50  0   0
60  0   0
80  0   0
120 0   0
90  0   0
TOTAL   0

Now You run solver form data tab and You get interface that You have to feed parameters:

Goal value is the CELL with the sum of all multiplications You are looking for exact value (type in 800)

By changing cels: select range of zeroes in second column

Add thre additional restriction (add button): range of zeroes have to be >= than 0 and <= 1 and int so we only have 0 and 1 as possible results (you have to reselect range every time you add another limitation)

Now press solve and after some time (depending on scale of your data sets ranging from seconds to many minutes) it will change some of the zeroes to 1 indicating which numbers where used to produce Your result.

If there are many possible outcomes it will choose one that he found without indicating that there are more, but running it again may produce different result.

Here is the result I got:

100 1   100
200 0   0
500 1   500
50  0   0
60  0   0
80  1   80
120 1   120
90  0   0
TOTAL   800

Solution 3:[3]

Help is on the way.

You can paste this function into a module and adjust it to your needs.

Function GetCombination(CoinsRange As Range, SumCellId As Range) As String
Dim Nb As Integer
Dim Com As String
Dim Sum As Double
Dim r As Range
Set r = CoinsRange
Sum = SumCellId.Value
For Each cell In r.Cells
If Sum / cell.Value >= 1 Then
Com = Com & Int(Sum / cell.Value) & " of " & cell.Value & "  "
Sum = Sum - (Int(Sum / cell.Value)) * cell.Value
End If
Next
GetCombination = Com
End Function

Preconditions:

  1. Coins or bills must be in descending order

My End result: enter image description here

Solution 4:[4]

I've been working on a windows application to do this. I'm close to a solution that I think will satisfy most people's needs.

The number of combinations is the issue for most algorithms, so the key is to ignore as many non-viable combinations as possible.

25 numbers in a list is approx 33 million combinations. 50 numbers in a list is a million millions of combinations. So, doing this in vba is probably not a viable option for most people, and solver won't deal with this very well either.

Brute force doesn't work because there are too many combinations if you're doing a list of more than 2 to 3 dozen numbers.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 C-Love511
Solution 2 Tetlanesh
Solution 3
Solution 4 Patrick