+ Reply to Thread
Results 1 to 11 of 11

Combination of Numbers Totalling Desired Sum

Hybrid View

  1. #1
    Registered User
    Join Date
    07-09-2010
    Location
    CT, USA
    MS-Off Ver
    Excel 2003
    Posts
    3

    Question Combination of Numbers Totalling Desired Sum

    I am not even sure there is a possible way to do this but here is my issue.

    I have a simple list with 2 columns which hold account numbers in column 1 and amounts in column 2. I have a total (sum) for 5 accounts which did not show on a report. So I need to find 5 accounts in a list of roughly 2300 accounts only knowing a total amount (sum) and that I need 5 amounts to make up this sum. Algebraically the formula would be x+y+z+a+b= c where c = predefined sum.

    I have tried looking at using some type of array sum function but nothing seems like it could fit. I also realize that there may be one or more combinations of 5 values that will equal the predefined sum. I am hoping to make some kind of loop that will continue until the possibilities are exhausted.

    I know this is ridiculous, but if it is possible I would like to try it.
    Attached Files Attached Files
    Last edited by contra19; 07-12-2010 at 08:55 AM. Reason: Request from moderators

  2. #2
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,627

    Re: [defined sum]=x+y+z+a+b

    Can you upload example workbook?

  3. #3
    Registered User
    Join Date
    07-09-2010
    Location
    CT, USA
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: [defined sum]=x+y+z+a+b

    example uploaded

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

    Re: [defined sum]=x+y+z+a+b

    Welcome to the forum.

    Please take a few minutes to read the forum rules, and then amend your thread title accordingly.

    Thanks.
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Registered User
    Join Date
    05-16-2010
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: I have a predetermined sum and I am trying to find the values that will make the

    Here are directions for use:

    Place list of numbers is Row 1 starting in Cell A1.
    Leave NO blank Cells.
    Zero is NOT allowed in Row 1.
    Numbers on list in Row 1 can be positive or negative.

    Place the number for Total in Cell A2.

    Run Sub AddCombo.
    Results appear beginning in Row 3.
    Will give any combinations on list with sum that results in Total.
    You can check for those that result from 5 accounts.
    Excel 2007 has enough Columns for this.

    Code below can go in a standard module.
    Code applies to the active sheet.

    This will clear the active sheet.
    This allows the column width to be set in the code with the Constant cKolWid.

    Option Explicit
    Const cKolWid As Long = 4
    Sub AddCombo()
    Dim dNeg As Double, dTotal As Double
    Dim lCol As Long, lJ As Long, lRw As Long
    Dim vIn As Variant, vRow As Variant
    dTotal = Range("A2").Value
    Rows(2).Clear
    With Range("A1").CurrentRegion
        lCol = .Columns.Count
        vIn = .Value
        Cells.ClearContents
        .Value = vIn
    End With
    Range("A2").Value = dTotal
    If lCol < 2 Then
        MsgBox "Must Be At Least Two On List"
        Exit Sub
    End If
    dNeg = 0
    For lJ = 1 To lCol
        If vIn(1, lJ) = 0 Then
            MsgBox "Zero Not Allowed In First Row"
            Exit Sub
        End If
        If vIn(1, lJ) < 0 Then dNeg = dNeg + vIn(1, lJ)
    Next lJ
    Application.ScreenUpdating = False
    For lJ = 1 To lCol
        Columns(lJ).ColumnWidth = cKolWid
    Next lJ
    If lCol < Columns.Count Then
        For lJ = 1 To 30
            Columns(lCol + 1).Delete
        Next lJ
    End If
    Application.ScreenUpdating = True
    ReDim vRow(1 To lCol)
    lRw = 0
    AddCombo1 vIn, vRow, dNeg, dTotal, 0, lRw, 1, lCol, False
    If dTotal = 0 Then Rows(3).Delete
    If lRw = 0 Then MsgBox "No Combinations Found"
    End Sub
    Sub AddCombo1(ByVal vIn As Variant, ByVal vRow As Variant, ByVal dNeg As Double, _
        ByVal dTotal As Double, ByVal dKalc As Double, ByRef lRw As Long, _
        ByVal lKol As Long, ByVal lCol As Long, ByRef bMax As Boolean)
    If bMax = True Then Exit Sub
    Dim lJ As Long
    For lJ = 0 To 1
        If lJ = 1 Then
            dKalc = dKalc + vIn(1, lKol)
            If vIn(1, lKol) < 0 Then dNeg = dNeg - vIn(1, lKol)
            If dKalc > dTotal - dNeg Then Exit Sub
            vRow(lKol) = vIn(1, lKol)
        End If
        If lKol = lCol Then
            If dKalc = dTotal Then
                lRw = lRw + 1
                If lRw > Rows.Count - 2 Then
                    bMax = True
                    MsgBox "All Rows Filled"
                    Exit Sub
                End If
                Range("A" & lRw + 2).Resize(1, lCol) = vRow
            End If
        Else
            AddCombo1 vIn, vRow, dNeg, dTotal, dKalc, lRw, lKol + 1, lCol, bMax
        End If
    Next lJ
    End Sub

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: I have a predetermined sum and I am trying to find the values that will make the

    Your post does not comply with Rule 1 of our Forum RULES. Your post title should accurately and concisely describe your problem, not your anticipated solution. Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.
    To change a Title on your post, click EDIT then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: I have a predetermined sum and I am trying to find the values that will make the

    Jack Bean
    Your post does not comply with Rule 7 of our Forum RULES. Please do not ignore Moderators' or Administrators' requests - note that this includes requests by senior members as well, if you are unclear about their request or instruction then send a private message to them asking for help. Do not post a reply to a thread where a moderator has requested an action that has not been complied with e.g Title change or Code tags...etc

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

    Re: Combination of Numbers Totalling Desired Sum

    Here's an add-in that may help.

    Unzip the file and save to some directory where you can find it. Do Tools > Add-Ins, browse to the directory, and select the file.

    Then do Alt+F8, type ComboSum in the Macro Name box, press Run, and follow the instructions.
    Attached Files Attached Files
    Last edited by shg; 07-11-2010 at 03:19 PM.

  9. #9
    Registered User
    Join Date
    07-09-2010
    Location
    CT, USA
    MS-Off Ver
    Excel 2003
    Posts
    3

    Combination of Numbers Totalling Desired Sum

    Thanks for all the help. I will try this out and hope to get the results I am looking for.
    Last edited by shg; 07-12-2010 at 09:25 AM.

  10. #10
    Registered User
    Join Date
    12-07-2010
    Location
    hawaii
    MS-Off Ver
    Excel 2003
    Posts
    1

    Re: Combination of Numbers Totalling Desired Sum

    Hey Shg,

    I was wondering where the results of this macro are displayed? I followed the prompted instructions for my listing of values and got no output.

  11. #11
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Combination of Numbers Totalling Desired Sum

    Quote Originally Posted by contra19 View Post
    I have a simple list with 2 columns which hold account numbers in column 1 and amounts in column 2. I have a total (sum) for 5 accounts which did not show on a report. So I need to find 5 accounts in a list of roughly 2300 accounts only knowing a total amount (sum) and that I need 5 amounts to make up this sum. Algebraically the formula would be x+y+z+a+b= c where c = predefined sum.

    I am hoping to make some kind of loop that will continue until the possibilities are exhausted.

    I know this is ridiculous
    It is ... number of possible combinations: 534033454837960
    It is not a problem to create a loop but it's unknown in how many hours or days you will get the result (all possible combinations for the amount)

+ 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