Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

How to find all combinations that equal a given sum in Excel?

For example, I have the following list of numbers, and now, I want to know which combination of numbers in the list sum up to 480, in the following screenshot shown, you can see there are five groups of possible combinations that add up equal to 480, such as 300+60+120, 300+60+40+80, etc. This article, I will talk about some methods to find which cells sum up to a specific value in Excel.

doc find combination equal sum 1

Find cells combination that equal a given sum with formulas

Find cells combination that equal a given sum with Solver Add-in

Find cells combination that equal a given sum with User Defined Function


List All Possible Combinations from multiple columns:

Kutools for Excel’s List All Combinations utility can help you quickly generate / list all possible combinations based on multiple lists of values.

doc find combination equal sum 2

Kutools for Excel: with more than 200 handy Excel add-ins, free to try with no limitation in 60 days. Download and free trial Now!


Find cells combination that equal a given sum with formulas


First, you need to create some range names, and then apply an array formula to find the cells that sum to target value, please do with the following step by step:

1. Select the number list and define this list a range name-- Range1 into the Name Box, and press Enter key to finish the range name defined, see screenshot:

doc find combination equal sum 2

2. After defining a range name for the number list, then you need to create two further range names in the Name Manager box, please click Formulas > Name Manager, in the Name Manager dialog box, click New button, see screenshot:

doc find combination equal sum 3

3. In the popped out New Name dialog, enter a name List1 into the Name field, and type this formula =ROW(INDIRECT("1:"&ROWS(Range1))) (Range1 is the range name you have created in step1) into the Refers to field, see screenshot:

doc find combination equal sum 4

4. Click OK to return to the Name Manager dialog, then continue to click New button to create another range name, in the New Name dialog, enter a name List2 into the Name field, and type this formula =ROW(INDIRECT("1:"&2^ROWS(Range1))) (Range1 is the range name you have created in step1) into the Refers to field, see screenshot:

doc find combination equal sum 5

5. After creating the range names, please apply the following array formula into cell B1:

=IF(ISNUMBER(MATCH(ROWS($1:1),IF(INDEX(MOD(INT((List2-1)/2^(TRANSPOSE(List1)-1)),2),MATCH(TRUE,MMULT(MOD(INT((List2-1)/2^(TRANSPOSE(List1)-1)),2),Range1)=$C$2,0),),TRANSPOSE(List1)),0)),"X",""), and press Shift + Ctrl + Enter keys together, then drag the fill handle down to cell B8, the last number of the list, and you can see the numbers which total amount is 480 are marked as X in column B, see screenshot:

doc find combination equal sum 6

Notes:

1. In the above long formula: List1, List2 and Range1 are the range names you have created in former steps, C2 is the specific value that you want numbers added up to.

2. If more than one combination of values has a sum equal to the specific value, only one combination is listed.


Find cells combination that equal a given sum with Solver Add-in

If you are confused with above method, Excel contains a Solver Add-in feature, by using this add-in, you can also identify the numbers which total amount equals a given value.

1. First, you need to activate this Solver add-in, please go to File > Options, in the Excel Options dialog box, click Add-Ins from the left pane, and then click Solver Add-in from the Inactive Application Add-ins section, see screenshot:

doc find combination equal sum 7

2. Then click Go button to enter the Add-Ins dialog, check Solver Add-in option, and click OK to install this add-in successfully.

doc find combination equal sum 8

3. After activating the Solver add-in, then you need to enter this formula into the cell B9: =SUMPRODUCT(B1:B8,A1:A8), (B1:B8 is a blank column cells beside your number list, and A1:A8 is the number list that you use. ), and press Enter key, see screenshot:

doc find combination equal sum 9

4. Then click Data > Solver to go to the Solver Parameter dialog box, in the dialog, please do the following operations:

(1.) Click doc button 1 button to select the cell B9 where your formula in from the Set Objective section;

(2.) Then in the To section, select Value Of, and enter your target value 480 as you need;

(3.) Under the By Changing Variable Cells section, please click doc button 1 button to select cell range B1:B8 where will mark your corresponding numbers.

doc find combination equal sum 10

5. And then click Add button to go to the Add Constraint dialog box, click doc button 1 button to select cell range B1:B8, and select bin from the drop down list, see screenshot:

doc find combination equal sum 11

6. Click OK to go back the Solver Parameter dialog, then click Solve button, some minutes later, a Solver Results dialog box is popped out, and you can see the combination of cells which equal a given sum 480 are marked as 1. In the Solver Results dialog, please select Keep Solver Solution option, and click OK to exit the dialog. See screenshot:

doc find combination equal sum 12

Note: This method is also only can get one combination cells if there are more than one combination of values has a sum equal to the specific value.


Demo: Find cells combination that equal a given sum in Excel


Find cells combination that equal a given sum with User Defined Function

The first two methods are all complex for most of our Excel users, here, I can create a VBA code to solve this job quickly and easily.

To get the correct result, you must sort the number list in descending order first. And then do with the following steps:

1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.

2. Click Insert > Module, and paste the following code in the Module Window.

VBA code: Find cells combination that equal a given sum:

Function GetCombination(CoinsRange As Range, SumCellId As Double) As String
'updateby Extendoffice 20160506
    Dim xStr As String
    Dim xSum As Double
    Dim xCell As Range
    xSum = SumCellId
    For Each xCell In CoinsRange
        If Not (xSum / xCell < 1) Then
            xStr = xStr & Int(xSum / xCell) & " of " & xCell & "  "
            xSum = xSum - (Int(xSum / xCell)) * xCell
        End If
    Next
    GetCombination = xStr
End Function

3. Then save and close this code window, and then go back to the worksheet, and enter this formula =getcombination(A1:A8,C2) into a blank cell, and press Enter key, you will get the following result which displays the combination numbers that equal a given sum, see screenshot:

doc find combination equal sum 13

Notes:

1. In the above formula, A1:A8 is the number range, and C2 contains the target value you want to equal to.

2. If more than one combination of values has a sum equal to the specific value, only one combination is listed.



Recommended Productivity Tools for Excel

Kutools for Excel Helps You Always Finish Work Ahead of Time, and Stand Out From Crowd

  • More than 300 powerful advanced features, designed for 1500 work scenarios, increasing productivity by 70%, give you more time to take care of family and enjoy life.
  • No longer need memorizing formulas and VBA codes, give your brain a rest from now on.
  • Become an Excel expert in 3 minutes, Complicated and repeated operations can be done in seconds, 
  • Reduce thousands of keyboard & mouse operations every day, say goodbye to occupational diseases now.
  • 110,000 highly effective people and 300+ world-renowned companies' choice.
  • 60-day full features free trial. 60-day money back guarantees. 2 years of free upgrade and support.

Brings Tabbed Browsing and Editing to Microsoft Office, Far More Powerful Than The Browser's Tabs

  • Office Tab is designed for Word, Excel, PowerPoint and Other Office Applications: Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Feroz · 1 months ago
    How do you do for the list of numbers like 480
  • To post as a guest, your comment is unpublished.
    Guilherme Dorn · 3 months ago
    Thank you very much! Resolved my problem correctly.
  • To post as a guest, your comment is unpublished.
    Miss Jones · 4 months ago
    Thank you so much for the VBA coding, it has solved a major headache trying to find combinations to equal an exact amount.
  • To post as a guest, your comment is unpublished.
    Stephanie · 5 months ago
    I have 1162 cells to find number x. Excel tells me that is too many variable cells. Very small data set! Any suggestions? Thanks!
  • To post as a guest, your comment is unpublished.
    a · 8 months ago
    Will the solver add-in not work if there are negative numbers in the list or if the value of number is 0? I'm trying to find a sum of numbers in a list that equate to zero with some numbers being negative and positive, but the solver does not work. I changed a couple numbers on my list to test to make sure I followed the steps correctly and it did work for the test. Please advise if there is a way to solve with negative and positive numbers to find a 0 value.