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
- Find all combinations that equal a given sum with an amazing feature
First, you need to create some range names, and then apply an array formula to find the cells that sum to the 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 screenshots:
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 B2:
=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:
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(B2:B9,A2:A9), (B2:B9 is a blank column cells beside your number list, and A2:A9 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 B10 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 B2:B9 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 B2:B9, 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:
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 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(A2:A9,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:
Maybe all of the above methods are somewhat difficult for you, here, I will introduce a powerful-tool, Kutools for Excel, with its Make Up A Number feature, you can quickly get all combinations that equal to a given sum.
After installing Kutools for Excel, please do as this:
1. Click Kutools > Content > Make Up A Number, see screenshot:
2. Then, in the Make up a number dialog box, please click button to select the number list that you want to use from the Data Source, and then enter the total number into the Sum textbox, see screenshot:
3. And then, click OK button, a prompt box will pop out to remind you to select a cell to locate the result, see screenshot:
4. Then, click OK, and now, all combinations that equal to that given number have been displayed as below screenshot shown:
Best Office Productivity Tools
Supports Office/Excel 2007-2021 and 365 | Available in 44 Languages | Easy to Uninstall Completely
Kutools for Excel Boasts Over 300 Features, Ensuring That What You Need Is Just A Click Away...
Supercharge Your Excel Skills: Experience Efficiency Like Never Before with Kutools for Excel (Full-Featured 30-Day Free Trial)
Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier
- Enable tabbed editing and reading in Word, Excel, PowerPoint, 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! (Full-Featured 30-Day Free Trial)