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.
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. 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:
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:
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:
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:
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:
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:
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.
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:
4. Then click Data > Solver to go to the Solver Parameter dialog box, in the dialog, please do the following operations:
(1.) Click 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 button to select cell range B1:B8 where will mark your corresponding numbers.
5. And then click Add button to go to the Add Constraint dialog box, click button to select cell range B1:B8, and select bin from the drop down list, see screenshot:
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:
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:
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
Office Tab
Bring handy tabs to Excel and other Office software, just like Chrome, Firefox and new Internet Explorer.
Kutools for Excel
Amazing! Increase your productivity in 5 minutes. Don't need any special skills, save two hours every day!
300 New Features for Excel, Make Excel Much Easy and Powerful:
- Merge Cell/Rows/Columns without Losing Data.
- Combine and Consolidate Multiple Sheets and Workbooks.
- Compare Ranges, Copy Multiple Ranges, Convert Text to Date, Unit and Currency Conversion.
- Count by Colors, Paging Subtotals, Advanced Sort and Super Filter,
- More Select/Insert/Delete/Text/Format/Link/Comment/Workbooks/Worksheets Tools...