+ Reply to Thread
Results 1 to 8 of 8

Trying every possible sum combination

Hybrid View

  1. #1
    Registered User
    Join Date
    05-28-2005
    Location
    WI, USA
    MS-Off Ver
    Office XP/2003
    Posts
    95

    Trying every possible sum combination

    Hi all,

    I have a project where I need to cycle through the number of rows to sum and which rows need summing.

    For example, if column G has six values in it, I want it to first look for single row that equal zero and delete them. After that, I want it to look at any two rows that when added equal zero, e.g. G1+G(2->6) then G2+G(3->6). If that doesn't work, I want it to look at three rows, e.g. G1+G2+G(3->6), G1+G3+G(4->6), etc, etc.

    I'm having a really hard time with the logic for this; everything I write duplicates the calc's or skips rows, etc. Does anyone have any code that already does something like this?


    Thanks!
    Chris

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Trying every possible sum combination

    How about posting a workbook with some examples?
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    05-28-2005
    Location
    WI, USA
    MS-Off Ver
    Office XP/2003
    Posts
    95

    Re: Trying every possible sum combination

    I've attached a sample of the data I'd be looking at. As you can see, there are 89 distinct values in column G, so there would be a lot of possible combinations. Hope this helps.
    Attached Files Attached Files

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Trying every possible sum combination

    So G2 contains 13,346.57, and you want to find one or more cells below that sum to -13,346.57, and zero all those out, then go down column G and repeat the process?

    That's a non-trivial exercise. Tushar Mehta wrote some code that will find combinations that add to a total. You'd have to wrap that with some more code.

  5. #5
    Registered User
    Join Date
    05-28-2005
    Location
    WI, USA
    MS-Off Ver
    Office XP/2003
    Posts
    95

    Re: Trying every possible sum combination

    That's exactly what I want to do. Do you know where I can find the code that he wrote ?

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Trying every possible sum combination

    Sub ComboSum(dTgt As Double, MaxSoln As Integer, rInp As Range)
        ' from http://www.tushar-mehta.com/excel/templates/match_values/index.html
    
        ' Lists the cells in single-column range rInp that total dTgt
        ' MaxSoln is the number of solutions wanted. Specify zero for all.
        ' Solutions are listed in the column to the right of rInp
        
        Const dEps  As Double = 0.00000001
        Const sSep  As String = ", "
    
        Dim asSoln() As String  ' solutions
        Dim vInp()  As Variant  ' values in rInp
        Dim daBeg   As Date     ' start time
    
        Dim WF      As WorksheetFunction
    
        If rInp.Columns.Count <> 1 Then Exit Sub
        Set WF = Application.WorksheetFunction
        daBeg = Now()
        
        ' get the values from the range
        vInp = WF.Transpose(WF.Transpose(WF.Transpose(rInp.Value)))
        ReDim asSoln(0 To 0)
    
        RecursiveMatch MaxSoln, dTgt, vInp(), LBound(vInp), 0, dEps, asSoln, "", sSep
    
        ' list the solutions
        asSoln(UBound(asSoln)) = "Done @ " & Format(Now(), "hh:mm:ss")
        rInp.Offset(, 1).Resize(UBound(asSoln) + 1, 1).Value = WF.Transpose(asSoln)
    End Sub
    
    Sub RecursiveMatch(ByVal MaxSoln As Integer, _
                       ByVal dTgt As Double, _
                       ByRef vInp() As Variant, _
                       ByVal iCurrInx As Integer, _
                       ByVal dCurrTot As Double, _
                       ByVal dEps As Double, _
                       ByRef asSoln() As String, _
                       ByVal sSoln As String, _
                       ByVal sSep As String)
    
        Dim i       As Integer
    
        For i = iCurrInx To UBound(vInp)
            If Abs(dCurrTot + vInp(i) - dTgt) <= dEps Then
                asSoln(UBound(asSoln)) = dCurrTot + vInp(i) _
                                         & sSep & Format(Now(), "hh:mm:ss") _
                                         & sSoln & sSep & CStr(i)
                If UBound(asSoln) Mod 100 = 0 Then Debug.Print UBound(asSoln) + 1 & " solutions ..."
                If MaxSoln <> 0 Then If UBound(asSoln) >= MaxSoln Then Exit Sub
                ReDim Preserve asSoln(UBound(asSoln) + 1)
    
            ElseIf dCurrTot + vInp(i) < dTgt - dEps And iCurrInx < UBound(vInp) Then
                RecursiveMatch MaxSoln, dTgt, vInp(), i + 1, _
                               dCurrTot + vInp(i), dEps, asSoln(), _
                               sSoln & sSep & CStr(i), sSep
                If MaxSoln <> 0 Then If UBound(asSoln) >= MaxSoln Then Exit Sub
    
            End If
        Next i
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1