How can I identify which cells sum to give a specific total from a list of numbers?
Find the sum of a specific value within multiple values
Looking for a specific sum within a range of numbers...
How to find numbers that sum up to a value (with a big data base)
i.e in the most simple example, if I have the following list of cells:
A1 - 3 A2 - 2 A3 - 3
How can I highlight which cells produce the sum of 5?
It's possible with the Solver add-in*. The following steps worked for me in Excel 2007 and 2010.
- Designate a cell to hold the result (C1 for this example) - this is the target cell, and a column that excel can use for scratch-work (B1:B100 for this example)
- In the target cell, enter the formula "=SUMPRODUCT(A1:A100,B1:B100)" (no quotes). This will calculate the sum of A1*B1+A2*B2+...etc
- Select Open the solver (Data tab, Analysis group)
- The target cell should be obvious (
1 for this example)
- For 'Equal To:' select 'Value of:' and enter the desired value
- In the 'By Changing Cells' enter "
1:
100" (no quotes, and it may be necessary to initialize these values to 0 yourself)
- Add a constraint to the cells that can be changed. In the pull-down, select 'bin' (Binary). This restricts the values of these cells to 0 (removing the corresponding A cell from the sum) or 1 (adding the corresponding A cell to the sum).
- Click 'Solve' and wait. The numbers that are part of the subset you're looking for will have a 1 in the B column

If the solver is taking a long time, you can help it out by removing rows that obviously won't work (total is in dollars, and only one row has nonzero cents)
Bonus: You can have excel automatically highlight the cells that you're looking for by adding conditional formatting to those cells. Select all of the cells you want to format and from (Home tab)>>(Styles group)>>Conditional formatting>>New Rule select 'Use a formula to determine which cells to format'. In the formula, enter '=$B1=1' (no quotes) which will evaluate to true if the corresponding row in the B column is 1. For the format, you can add whatever you want (bold, italic, green fill, etc).
Another easy way to find the important rows is to sort column B Z->A, and all the 1's will come to the top.
*The solver add-in can be installed with these steps
- Click the Microsoft Office Button, and then click Excel Options.
- Click Add-Ins, and then in the Manage box, select Excel Add-ins.
- Click Go.
- In the Add-Ins available box, select the Solver Add-in check box, and then click OK. (If Solver Add-in is not listed in the Add-Ins available box, click Browse to locate the add-in.)
- If you get prompted that the Solver Add-in is not currently installed on your computer, click Yes to install it.
There is a low cost Excel Add-in SumMatch, which will highlight the subset of numbers that add up to a target sum.

I have 100 numbers from 400 to 3500 and need to fund which numbers that, if you sum up, gives me a total of 3320.
Example: 2 + 3 = 5 (need to find the numbers 2 and 3 on excel)
I used the formula Solver but the problem is that it takes ages, I used on my sheet and it's has been more than an 1 hour and it's still processing!!!. It stopped twice and this is my third time trying it.
Is there a faster way? Or any online site that does that? I need to finish the report by tomorrow and have no clue... the numbers 400 to 3500 are values ($). Any help is appreciated
I am trying to see which numbers out of a list, equal the sum of $1039.70
How do I accomplish this?
I don't know if there is a way to do this in Excel but trying to do it manually is most time consuming. I am under a deadline to reconcile payments. For example, I have a value of $69.05 that was billed but is made up of multiple items. I am trying to find the amounts from the list that add up to $69.05. I would love to be able to automate this!
I have managed to make a VBA solution for this. I have tested with multiple different goals/targets & different range of numbers to sum and worked every time. Can't guarantee there isn't a problem it won't work around though.
Here it is:
Note - you should be able to now have multiple of the same number. This will only return the first solution it finds. It doesn't find EVERY solution.
Sub SumSolver()
Dim rng, Goal As Double, ws As Worksheet, i As Long, j As Long, Answer As Double, k As Long
Dim lRow As Long, Answerlist As String, LastAdded As Long, AnswerListPos As String
Dim c As Range, RngToSplit As String, AnswerArray, AnswerItem
Set ws = Sheets("Sheet1") 'Change Sheet1 to your sheet name
lRow = ws.Range("B" & ws.Rows.Count).End(xlUp).Row 'Change to needed column
ws.Range("C2:C" & lRow).ClearContents 'Clear output range if needed
For Each c In ws.Range("B2:B" & lRow) 'This loop populates the list range into a string
If c.Value <> "" Or Not IsNumeric(c.Value) Then 'Checking for empty or non-numeric values
If RngToSplit = "" Then
RngToSplit = c.Value
Else
RngToSplit = RngToSplit & "," & c.Value
End If
End If
Next
rng = Split(RngToSplit, ",") 'Split the new list string into an array
If Not IsNumeric(ws.Range("A2").Value) Then 'Checks target value is actually a number
MsgBox "The target value is not a valid number. Please correct this before trying again.", vbExclamation, "Sum Solver"
Exit Sub
Else
Goal = ws.Range("A2").Value 'Value of the goal/target
End If
For i = 0 To UBound(rng) ' 0 = start of array, Ubound = End of array
If rng(i) = Goal Then
ws.Range("C2") = rng(i)
Answerlist = rng(i)
GoTo SubExit
ElseIf rng(i) < Goal Then
Answer = rng(i)
Answerlist = rng(i)
AnswerListPos = i
For j = i + 1 To UBound(rng)
If Answer + rng(j) = Goal Then
Answerlist = Answerlist & "," & rng(j)
AnswerListPos = AnswerListPos & "," & j
GoTo SubExit
ElseIf Answer + rng(j) < Goal Then
Answer = Answer + rng(j)
LastAdded = j
If Answerlist = "" Then
Answerlist = rng(j)
AnswerListPos = j
Else
Answerlist = Answerlist & "," & rng(j)
AnswerListPos = AnswerListPos & "," & j
End If
End If
If j = UBound(rng) Then
If LastAdded = UBound(rng) Then
Answerlist = Left(Answerlist, InStrRev(Answerlist, ",") - 1)
AnswerListPos = Left(AnswerListPos, InStrRev(AnswerListPos, ",") - 1)
Answer = Answer - rng(j)
LastAdded = Val(Mid(AnswerListPos, InStrRev(AnswerListPos, ",") + 1))
End If
If LastAdded > 0 Then Answer = Answer - rng(LastAdded)
If InStr(Answerlist, ",") = 0 Then Exit For
j = Val(Mid(AnswerListPos, InStrRev(AnswerListPos, ",") + 1))
Answerlist = Left(Answerlist, InStrRev(Answerlist, ",") - 1)
AnswerListPos = Left(AnswerListPos, InStrRev(AnswerListPos, ",") - 1)
End If
Next j
End If
Answerlist = ""
Next i
SubExit:
If Answerlist <> "" Then
i = 2
AnswerArray = Split(Answerlist, ",") 'Split the result into an array
For Each AnswerItem In AnswerArray
ws.Range("C" & i) = AnswerItem 'Output the results into the sheet
i = i + 1
Next
Else
MsgBox "No possible combination found for a target value of " & Goal & ".", vbExclamation, "Sum Solver"
End If
End Sub
EDIT: Just updated to account for if there are any blank rows in the list range as well as handle if a value is non-numeric. Actually made it half a second faster (13s) for a 12 item list of 1000 iterations.
You will see the rows I've made comments on are the ones you need to change. Pretty much just what columns it is working on and the starting cell It works looking at the last row but if you don't need it then just replace for example "B2:B" & lRow with B2:B5 etc.
I've also incorporated it into a function. Used as:
=SumSolver(Target value, Range of sum values)
It returns the results in the same cell separated by a comma. This can be changed to another method easily if needed though.
Function SumSolver(Goal As Double, ListRange As Range)
Dim i As Long, j As Long, Answer As Double, k As Long, rng As Variant
Dim Answerlist As String, LastAdded As Long, AnswerListPos As String
rng = Application.Transpose(ListRange)
For i = 1 To UBound(rng)
If rng(i) = Goal Then
Answerlist = rng(i)
GoTo SubExit
ElseIf rng(i) < Goal Then
Answer = rng(i)
Answerlist = rng(i)
AnswerListPos = i
For j = i + 1 To UBound(rng)
If Answer + rng(j) = Goal Then
Answerlist = Answerlist & "," & rng(j)
AnswerListPos = AnswerListPos & "," & j
GoTo SubExit
ElseIf Answer + rng(j) < Goal Then
Answer = Answer + rng(j)
LastAdded = j
If Answerlist = "" Then
Answerlist = rng(j)
AnswerListPos = j
Else
Answerlist = Answerlist & "," & rng(j)
AnswerListPos = AnswerListPos & "," & j
End If
End If
If j = UBound(rng) Then
If LastAdded = UBound(rng) Then
Answerlist = Left(Answerlist, InStrRev(Answerlist, ",") - 1)
AnswerListPos = Left(AnswerListPos, InStrRev(AnswerListPos, ",") - 1)
Answer = Answer - rng(j)
LastAdded = Val(Mid(AnswerListPos, InStrRev(AnswerListPos, ",") + 1))
End If
If LastAdded > 0 Then Answer = Answer - rng(LastAdded)
If InStr(Answerlist, ",") = 0 Then Exit For
j = Val(Mid(AnswerListPos, InStrRev(AnswerListPos, ",") + 1))
Answerlist = Left(Answerlist, InStrRev(Answerlist, ",") - 1)
AnswerListPos = Left(AnswerListPos, InStrRev(AnswerListPos, ",") - 1)
End If
Next j
End If
Answerlist = ""
Next i
SubExit:
If Answerlist <> "" Then
SumSolver = Answerlist
Else
SumSolver = "N/A"
End If
End Function
Example:

I decided to do an update of my speed test. This time comparing the updated code against my original, and was surprised to see the difference. I did runs of 1000 iterations where the solver could not find a combination. I did this with screen updating on. For a list of 8 there is a max of 255 combinations, for a list of 12, there is a max of 4095 combinations (doubles with each item added). For a list of 12, that comes to 4,095,000 calculations. Updated code did that in an average of 13.6 seconds. Obviously this is on my machine, which will have different results to yours (but the ratios should still be about the same).

So, just like this:

=sumproduct(C5:C10,D5:D10)
is the formula in cell B8 and is the objective in the Solver.
The two constraints control the model and DO check the options to make sure "Ignore Integer" is NOT selected.
Binary is used as this is a selected or not selected situation.
I used equal to in the constraint, but in some situations you may need to use <= or >= as there may not be an exact solution.
The Solver also deals with multiple values the same, unlike the other answer, however, as there are multiple solutions then which ones are picked are random:
