Which numbers add up to total? (2): Multiple Solutions 14

Note to readers: this post has been updated due to the inclusion – at the request of Torstein – of a further version of this solution, in which the number of values to be considered is dynamic and so may be set by the user. This version may be found at the very end of this post.

This post, inspired by a question from Patrick MacKay, from Belgium – thanks, Patrick! 🙂 – is a (rather belated) follow-up to that which I made here, in which, to recap, I presented a formula-based set-up which, given a target figure plus a series of values, determined which, if any, combination of those values had a sum equal to the target.

The only slight drawback to that solution was the caveat that, if more than one combination of values existed which satisfied that condition, then only one of those combinations was given.

Here I would like to improve upon that set-up by presenting a refined version which will return all such combinations. What’s more, at the very end of this deconstruction I will give a further version of the solution in which the number of values to be considered is a variable which may be set by the user.

In fact, that early post was also one of the very few in which I did not give an explanation as to how the solution works, which I would like to do here.

As an example of the output, imagine that our target value – £1054.35, for example – is here in A1, and that we have a list of 10 values in A2:A11, as below:

Which Numbers Add Up To Total (Multiple Solutions)

(You can download the workbook here.)

Columns B to K represent the (up to 10) solutions, indicated by an “X” appearing next to one of the 10 values in A2:A11 if that value forms part of a combination of those values whose sum is equal to the value in A1. Here it can easily be verified that there are 3 such solutions, since each of:


does indeed give 1054.35.

Before I begin, it is worth reiterating the limitation inherent in this solution due to the resource-heavy nature of the formula-work involved. The set-up has a theoretical limit on the size of the dataset of 20 values. However, it is more than likely that Excel will run out of resources in that case due to the sheer size of the calculations involved. Indeed, 15 appears to be an approximate upper bound on the number of values to be considered using this set-up: any more than that and Excel may begin to struggle, and so readers hoping for a solution with a dataset of such a size should probably seek a VBA-based solution instead.

The required solution is as follows: first we go to Name Manager and define:

Values as:


Arry1 as:


Arry2 as:


The array formula** in L1, which determines the number of permutations of summations which meet our target value (and so can be referenced in our main formulas so as to avoid resource-heavy IFERROR approaches), is:


And the array formula** in B2 is then:


and copied to the right and down.

How does it work?

Let’s take one of the formulas which results in a positive return (i.e. results in an “X”) in order to see how this construction operates. Taking the formula in B5, for example, i.e.:


(Clearly the only part having changed being the reference being passed to the ROWS function.)

Let’s have a look in detail at this part:


since this construction forms the crux of the solution.

In order to understand the functioning of this composition, it will be useful to look at a version which results in an array of smaller dimensions, not least because an attempt to reproduce in full the result of the above – a 1024-row-by-10-column (!) matrix – would make things a touch problematic.

Temporarily, then, and just for the purpose of aiding the deconstruction of this part of the formula, let’s assume that our range of possible values consisted of, not 10, but a mere 4 entries, i.e. that Values was defined, not as:


but rather as:


This being the case, we see that our Defined Name Arry1, which recall is:


would then resolve as:




and Arry2, which we defined as:


would resolve as:


which is:




Our construction, which is:


is thus here:


which becomes, resolving the TRANSPOSE and the subtraction of 1 from the resulting array:


and then resolving 2 to the power of this array:


and subtracting 1 from the first array:


We now perform the array of divisions, bearing in mind that, the two arrays here being orthogonal (the numerator being a 16-element single-column vector, the denominator a 4-element single-row vector), we know that the resulting array will comprise a 16-row-by-4-column array, the 64 entries in which being the result of dividing each of the 16 entries in the first array by each of the 4 entries in the second, the result being:


Just to take a quick example as way of further clarification, the 5th row in this array, i.e. {4,2,1,0.5}, is the result of dividing the 5th member of the first array, i.e. 4, by each of 1, 2, 4 and 8 respectively.

We then take the integer portion only of each of these values, so that we now have:


and then finally generate the results of this array modulo 2:


And if I just re-format this array so that it is clearer what is being represented:

0 0 0 0
1 0 0 0
0 1 0 0
1 1 0 0
0 0 1 0
1 0 1 0
0 1 1 0
1 1 1 0
0 0 0 1
1 0 0 1
0 1 0 1
1 1 0 1
0 0 1 1
1 0 1 1
0 1 1 1
1 1 1 1

then hopefully readers will be able to see that we have succeeded in creating an array which consists of all 16 permutations of a four-element set where the values within that set are either 0 or 1.

And the justification for having created such an array is that it gives us the means, once we arrange the appropriate matrix multiplication, to generate (and sum) all possible combinations from our entries in the Values range.

For example (and recall that we temporarily reduced our Values array to one of just four cells, i.e. A2:A5), the matrix product of the fourth row in this array and the entries in Values, i.e. of:




which we would achieve in Excel by:


will give us the sum of the first and second entries (283.75 and 350.25) within our Values array.

Or, to take another example, the matrix product of the fifteenth row in this array and the entries in Values, i.e. of:




which is, in Excel:


will give us the sum of the second, third and fourth entries (350.25, 290.27 and 246.89) within our Values array.

Since we have accounted for all permutations of 1s and 0s, we guarantee that all possible summations of values from our range are calculated, which of course is precisely what this challenge is all about.

Although we looked at the reduced case for a Values range comprising four cells, it can easily be verified that this construction is sufficiently dynamic as to work for ranges of other sizes too (within limits – see below). For example, if Values were instead a 5-cell range, e.g. A2:A6, then this construction:


would this time resolve to the following array:


or, more helpful visually:

0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
1 1 0 0 0
0 0 1 0 0
1 0 1 0 0
0 1 1 0 0
1 1 1 0 0
0 0 0 1 0
1 0 0 1 0
0 1 0 1 0
1 1 0 1 0
0 0 1 1 0
1 0 1 1 0
0 1 1 1 0
1 1 1 1 0
0 0 0 0 1
1 0 0 0 1
0 1 0 0 1
1 1 0 0 1
0 0 1 0 1
1 0 1 0 1
0 1 1 0 1
1 1 1 0 1
0 0 0 1 1
1 0 0 1 1
0 1 0 1 1
1 1 0 1 1
0 0 1 1 1
1 0 1 1 1
0 1 1 1 1
1 1 1 1 1

whose 32 rows readers may like to verify are precisely the 32 possible permutations of zeroes and ones for a 5-character string.

This construction for generating such arrays of permutations is extremely useful, albeit one which is restricted by the limitations imposed on certain functions (i.e. ROW, the range passed being limited to a maximum value equivalent to the number of rows in a worksheet, i.e .(post-2003) 2^20). Which explains my comment in the preamble, since, with this restriction, we are unfortunately not able to extend this construction so as to work for ranges comprising more than 20 values.

Anyway, returning to our deconstruction, we now see that, in full:


will return a 1024-element single-column array consisting of the sums of all possible permutations from our entries in Values (if readers weren’t already aware, 1024=2^10, 10 being the number of elements in our range), viz (I will not present all one thousand-plus entries for obvious reasons; just the first 50 entries should suffice to illustrate):


I have highlighted the one value in this array which is equal to our target value (a further two would be seen had I given the array in full, the 485th and 678th elements, to be precise).

We thus query this array of summations as to which, if any, is equal to the target value, such that:


which is now:




We then return the position of each of our Boolean TRUE returns within this array, so that:


which is:




i.e. 27.

And so we know that the 27th of our 1024 summations which we formed is one which does indeed result in our desired total.

We now need to return to that array of 1024 permutations to find out which exact values were involved in that summation. Hence, we INDEX that array – which recall is a 1024-row-by-10-column matrix – with this value of 27 as the row parameter, such that:


which is:




which array corresponds to taking the values from A3, A5 and A6 from our range, which do indeed sum to our target value.

The rest is relatively straightforward: we simply check whether the row in which the formula is placed corresponds to a non-zero value in this array, such that:


which is here:






which is “X”, as required.

Finally, and thanks to Torstein for being the inspiration behind this addition, here is an amended version which has the added feature that the number of values to be considered is dynamic, and so can be defined by the user.

Here, then, we may enter this limiting value into e.g. L2, as below:

Which Numbers Add Up To Total (Multiple Solutions - Restricted Number of Values)

(You can download the workbook here.)

Here, this value in L2 represents the fact that we wish to only consider combinations of precisely 3 values from our 10 in A2:A11. Hence each of the 8 possible solutions that you can see does indeed consist of that number of values.

The required set-up this time is:

The array formula** in L1 is now:


and that in B2:


Many thanks to Torstein and Oscar! Another post to follow shortly. Watch this space!


  1. I posted this comment on get-digital-help.com but maybe you do not read it. Thays why I comment here too.

    Hi Oscar and XOR LX!

    I’m really impressed what you guys can do with excel formulas. I have been trying to figure out permutations for so long, but have not been able to make it.

    One question to the formulas: Is it possible to restrict solutions to those which have eg three numbers that adds to the total?

    Hope I make my self understood!

    Regards Torstein

  2. Torstein

    Welcome to the site and thanks for your kind words!

    It’s nice to know that some of the work I do here is of practical interest to some.

    As to your question: yes, this is possible. We could do it by first restricting the array of permutations of 1s and 0s such that each permutation contains precisely three 1s.

    I’m a touch busy at the moment, but if you bear with me then I’ll look into this and post a reply here when I’ve worked out the necessary amendments for this case.


  3. Une remarque quand même: c’est plus rapide par VBA et le pc n’est pas ralenti par les formules matricielles, mais ici, c’est un superbe travail 🙂


  4. That’s great work you did, mate. Congrats!

    How many rows does your spreadsheet support? Is it limited to 10/11?

  5. @Leeroy

    Thanks! As I mention in the post, unfortunately this set-up has a limit of 20 values to be considered, though you may well find that calculation time beyond, say, 15 values, will be unfeasibly long.


  6. Awesome solution. However, when I expand the values range up to A500 (my data would have sometimes reach that number of rows), the formula will just ave me #ref.

  7. Hi, I want to find the numbers whose sum is less than or equal to say 5% between target and actual sum. Suppose 180 is target. I am satisfied if the output is more than 176 and equal to or less than 180. I need multiple solutions and the solution must have different combination of numbers.


  8. Hi Shreyans,

    And how many numbers are involved here? Have you read my article, in particular the part which mentions that my solution has a theoretical limit of 20 numbers?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.