Find the cells contain value sum up to a target value

eyelash

New Member
Joined
Apr 20, 2012
Messages
7
My work is still using MS2016, and the Solver is disabled. I need to find the transactions that sum up to a targeted value.
For instance: Col A has following amount, and the target amount is 90.23
15.23
50.00
566.45
74.20
13.45
45.00
66.78
0.23
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
If you Google "Subset sum problem", you'll find many articles related to your query.
 
Upvote 0
If you Google "Subset sum problem", you'll find many articles related to your query.
Thank you! I tried searching "Subset sum problem" in Google 😝, and find a lots of interesting articles and videos related to.

However, so far, they only work for the whole number, won't work for decimal or negative numbers.
 
Upvote 0
Maybe you didn't look hard enough ;).
Answer from StackOverflow.
VBA CODE:
VBA Code:
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

Book1
ABCDEFG
1ListTargetAnswerListTargetAnswer
215.2349.2-25,74.2-10-4-10,-2,8
350-2
4-2516
574.28
613.45
745
866.78
90.23
Sheet6
Cell Formulas
RangeFormula
C2C2=SumSolver(B2,A2:A9)
G2G2=SumSolver(F2,E2:E5)
 
Upvote 0

Forum statistics

Threads
1,223,992
Messages
6,175,827
Members
452,673
Latest member
LaMiaAvy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top