Results 1 to 10 of 10

Thread: VBA get target sum from the grid of 7*9

  1. #1
    Board Regular
    Join Date
    Feb 2008
    Posts
    1,752
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default VBA get target sum from the grid of 7*9

    Hello,

    I need a macro that allow me to create target sum from the grid of 7 columns and 9 rows...using technique picking a one number from each columns and making all possible targets sum can be made in the "as per specified sum target in the column S"

    Example below shows only one set of sum 17 (in the columns L:R) but I hope could be made much more than ones...

    ABCDEFGHIJKLMNOPQRS
    1
    2
    3
    4
    5n1n2n3n4n5n6n7n1n2n3n4n5n6n7Sum
    6-4 8 1 4 -3 8 8 -273-223617
    72 -6 5 -2 6 -2 -4 ???????17
    87 1 3 8 8 -5 5 ???????17
    94 -2 8 3 2 -3 -2 ???????17
    100 4 7 6 -10 6 4 ???????17
    113 6 -32 1 -1 -8 2 ???????17
    126 2 2 -16 7 3 6 ???????17
    138 7 -16 7 3 7 -9 ???????17
    14-2 -18 4 -32 5 2 -3
    15
    16

    Sheet1



    Worksheet Formulas
    CellFormula
    S6=SUM(L6:R6)



    Thank you all

    Excel 2000
    Regards,
    Moti
    Last edited by motilulla; Sep 10th, 2019 at 12:28 PM.

  2. #2
    MrExcel MVP Oaktree's Avatar
    Join Date
    Jun 2002
    Location
    Chicago, IL
    Posts
    7,931
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    1 Thread(s)

    Default Re: VBA get target sum from the grid of 7*9

    I think this is similar to a Mr Excel Challenge from several years ago.

    See:

    https://www.mrexcel.com/challenges/a...ble-challenge/

    for the solution Bill picked and lots of discussion about this type of problem.
    "The greatest challenge to any thinker is stating the problem in a way that will allow a solution." Bertrand Russell

  3. #3
    MrExcel MVP Eric W's Avatar
    Join Date
    Aug 2015
    Location
    Bountiful, UT
    Posts
    8,628
    Post Thanks / Like
    Mentioned
    42 Post(s)
    Tagged
    5 Thread(s)

    Default Re: VBA get target sum from the grid of 7*9

    It is similar to that, but this has the added constraint that you must take one value from each column. There are therefore 9^7 possible combinations, or 4,782,969. A big number, but not too bad for a reasonably fast computer. One big similarity to the challenge is that the number of results is really far too big to really do anything with. This particular problem has 93,063 solutions. Here's the code:

    Code:
    Sub Sub97()
    Dim r As Long, c As Long, Dict As Object, grid As Variant, ix() As Long
    Dim s As Long, sc As String, target As Long, Output(), x As Variant, y As Variant
    
    
        Set Dict = CreateObject("Scripting.Dictionary")
        
        target = Range("S6").Value
        grid = Range("C6:I14")
        r = UBound(grid, 1)
        c = UBound(grid, 2)
        ReDim ix(1 To c)
        
        For i = 1 To c
            ix(i) = 1
        Next i
        
    ChkAgain:
        s = 0
        sc = ""
        For i = 1 To c
            s = s + grid(ix(i), i)
            sc = sc & grid(ix(i), i) & IIf(i < c, "|", "")
        Next i
        If s = target Then Dict(sc) = 1
        
        For i = 1 To c
            ix(i) = ix(i) + 1
            If ix(i) <= r Then GoTo ChkAgain:
            ix(i) = 1
        Next i
        
        ReDim Output(1 To Dict.Count, 1 To c)
        r = 1
        For Each x In Dict
            y = Split(x, "|")
            For i = 1 To c
                Output(r, i) = y(i - 1)
            Next i
            r = r + 1
        Next x
        
        Range("L6").Resize(Dict.Count, c) = Output
            
    End Sub
    It pulls the table from C6:I14. It pulls the target value from S6. (The red values.) But you can change them in the code, and the rest will adapt to the new sizes. This ran in about 30 seconds on my PC.
    Last edited by Eric W; Sep 10th, 2019 at 02:08 PM.
    Cheers,
    Eric

    When you eliminate the impossible, whatever remains, however improbable, must be the truth.

    -Posting guidelines, forum rules, terms of use, FAQs, BB codes, See how to search the forum
    -Post a screen shot with the HTML Maker

  4. #4
    Board Regular
    Join Date
    Feb 2008
    Posts
    1,752
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA get target sum from the grid of 7*9

    Quote Originally Posted by Eric W View Post
    It is similar to that, but this has the added constraint that you must take one value from each column. There are therefore 9^7 possible combinations, or 4,782,969. A big number, but not too bad for a reasonably fast computer. One big similarity to the challenge is that the number of results is really far too big to really do anything with. This particular problem has 93,063 solutions. Here's the code:

    Code:
    Sub Sub97()
    Dim r As Long, c As Long, Dict As Object, grid As Variant, ix() As Long
    Dim s As Long, sc As String, target As Long, Output(), x As Variant, y As Variant
    
    
        Set Dict = CreateObject("Scripting.Dictionary")
        
        target = Range("S6").Value
        grid = Range("C6:I14")
        r = UBound(grid, 1)
        c = UBound(grid, 2)
        ReDim ix(1 To c)
        
        For i = 1 To c
            ix(i) = 1
        Next i
        
    ChkAgain:
        s = 0
        sc = ""
        For i = 1 To c
            s = s + grid(ix(i), i)
            sc = sc & grid(ix(i), i) & IIf(i < c, "|", "")
        Next i
        If s = target Then Dict(sc) = 1
        
        For i = 1 To c
            ix(i) = ix(i) + 1
            If ix(i) <= r Then GoTo ChkAgain:
            ix(i) = 1
        Next i
        
        ReDim Output(1 To Dict.Count, 1 To c)
        r = 1
        For Each x In Dict
            y = Split(x, "|")
            For i = 1 To c
                Output(r, i) = y(i - 1)
            Next i
            r = r + 1
        Next x
        
        Range("L6").Resize(Dict.Count, c) = Output
            
    End Sub
    It pulls the table from C6:I14. It pulls the target value from S6. (The red values.) But you can change them in the code, and the rest will adapt to the new sizes. This ran in about 30 seconds on my PC.
    Eric, thank you so much for the code it works fine when set are less than 65531 rows in other case it highlights following line "Range("L6").Resize(Dict.Count, c) = Output" and give the error 1004, I did test with sum -27 it create 32928 set perfectly

    As with the sum 17 there is 93,063 sets can't complete it due to version 2000 row limits does it has some solution for it. Please advice.


    Kind Regards,

    Moti

  5. #5
    MrExcel MVP Eric W's Avatar
    Join Date
    Aug 2015
    Location
    Bountiful, UT
    Posts
    8,628
    Post Thanks / Like
    Mentioned
    42 Post(s)
    Tagged
    5 Thread(s)

    Default Re: VBA get target sum from the grid of 7*9

    What do you want to do with it? There's really no reason to display all 93,063 combinations. That's far too many to manually examine. If you have some way of sifting through the list and picking out some, let me know and I can apply it as part of the macro. If you just want the count, I can display the count and the first 65,000 combinations. If you have some strange reason for wanting to actually show them all, I could put 65K in L:R, 65K in T:Z, 65K in AB:AH, etc. Excel 2000 has 256 columns, so that would give you up to almost 2 million combinations. But that's overkill.
    Last edited by Eric W; Sep 10th, 2019 at 06:10 PM.
    Cheers,
    Eric

    When you eliminate the impossible, whatever remains, however improbable, must be the truth.

    -Posting guidelines, forum rules, terms of use, FAQs, BB codes, See how to search the forum
    -Post a screen shot with the HTML Maker

  6. #6
    Board Regular
    Join Date
    Feb 2008
    Posts
    1,752
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA get target sum from the grid of 7*9

    Eric, thank you very much for your response
    Quote Originally Posted by Eric W View Post
    What do you want to do with it? There's really no reason to display all 93,063 combinations.
    I totally agree with you I did not new it could be so much but now problem is that I cannot list any. And get the error 1004 so far I cannot examine nothing. Also as you said there could be possible combinations 4,782,969 so do I can know? How much could be with each sum as for example you test with sum 17=93,063, and I did test with sum -27 it create 32928 set perfectly.

    Quote Originally Posted by Eric W View Post
    That's far too many to manually examine. If you have some way of sifting through the list and picking out some, let me know and I can apply it as part of the macro. If you just want the count, I can display the count and the first 65,000 combinations.
    Really as never generated non-to analysis no idea.

    Quote Originally Posted by Eric W View Post
    If you have some strange reason for wanting to actually show them all, I could put 65K in L:R, 65K in T:Z, 65K in AB:AH, etc. Excel 2000 has 256 columns, so that would give you up to almost 2 million combinations. But that's overkill.
    In this case I think would be better to have a list total combinations with each sum could be...and than pick one sum to generate all with selected sum if are more than 65000 than go to as you suggest. "You could put 65K in L:R, 65K in T:Z, 65K in AB:AH, etc."

    I imagine it needs a lot of work to solve in this way. My request is to you if possible please can you do it for me. No problem of time when you can
    thank you

    Kind Regards,

    Moti

  7. #7
    MrExcel MVP Eric W's Avatar
    Join Date
    Aug 2015
    Location
    Bountiful, UT
    Posts
    8,628
    Post Thanks / Like
    Mentioned
    42 Post(s)
    Tagged
    5 Thread(s)

    Default Re: VBA get target sum from the grid of 7*9

    Try:
    Code:
    Sub Sub97()
    Dim r As Long, c As Long, Dict As Object, grid As Variant, ix() As Long, outrange as Range
    Dim s As Long, sc As String, target As Long, Output(), x As Variant, y As Variant
    
        Set Dict = CreateObject("Scripting.Dictionary")
        
        target = Range("S6").Value
        grid = Range("C6:I14")
        Set outrange = Range("L6")
        
        r = UBound(grid, 1)
        c = UBound(grid, 2)
        ReDim ix(1 To c)
        
        For i = 1 To c
            ix(i) = 1
        Next i
        
    ChkAgain:
        s = 0
        sc = ""
        For i = 1 To c
            s = s + grid(ix(i), i)
            sc = sc & grid(ix(i), i) & "|"
        Next i
        If s = target Then Dict(sc) = 1
        
        For i = 1 To c
            ix(i) = ix(i) + 1
            If ix(i) <= r Then GoTo ChkAgain:
            ix(i) = 1
        Next i
        
        r = 0
        ReDim Output(1 To 65000, 1 To c)
        For Each x In Dict
            y = Split(x, "|")
            r = r + 1
            For i = 1 To c
                Output(r, i) = y(i - 1)
            Next i
            If r = 65000 Then
                outrange.Offset(-1).Resize(1, c) = Range("C5:I5").Value
                outrange.Resize(65000, c) = Output
                ReDim Output(1 To 65000, 1 To c)
                r = 0
                Set outrange = outrange.Offset(, c + 1)
            End If
        Next x
        
        If r > 0 Then
            outrange.Offset(-1).Resize(1, c) = Range("C5:I5").Value
            outrange.Resize(65000, c) = Output
        End If
            
    End Sub
    Values in red would need to be changed if you ever rearrange your sheet.
    Cheers,
    Eric

    When you eliminate the impossible, whatever remains, however improbable, must be the truth.

    -Posting guidelines, forum rules, terms of use, FAQs, BB codes, See how to search the forum
    -Post a screen shot with the HTML Maker

  8. #8
    Board Regular
    Join Date
    Feb 2008
    Posts
    1,752
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA get target sum from the grid of 7*9

    Quote Originally Posted by Eric W View Post
    Try:
    Code:
    Sub Sub97()
    Dim r As Long, c As Long, Dict As Object, grid As Variant, ix() As Long, outrange as Range
    Dim s As Long, sc As String, target As Long, Output(), x As Variant, y As Variant
    
        Set Dict = CreateObject("Scripting.Dictionary")
        
        target = Range("S6").Value
        grid = Range("C6:I14")
        Set outrange = Range("L6")
        
        r = UBound(grid, 1)
        c = UBound(grid, 2)
        ReDim ix(1 To c)
        
        For i = 1 To c
            ix(i) = 1
        Next i
        
    ChkAgain:
        s = 0
        sc = ""
        For i = 1 To c
            s = s + grid(ix(i), i)
            sc = sc & grid(ix(i), i) & "|"
        Next i
        If s = target Then Dict(sc) = 1
        
        For i = 1 To c
            ix(i) = ix(i) + 1
            If ix(i) <= r Then GoTo ChkAgain:
            ix(i) = 1
        Next i
        
        r = 0
        ReDim Output(1 To 65000, 1 To c)
        For Each x In Dict
            y = Split(x, "|")
            r = r + 1
            For i = 1 To c
                Output(r, i) = y(i - 1)
            Next i
            If r = 65000 Then
                outrange.Offset(-1).Resize(1, c) = Range("C5:I5").Value
                outrange.Resize(65000, c) = Output
                ReDim Output(1 To 65000, 1 To c)
                r = 0
                Set outrange = outrange.Offset(, c + 1)
            End If
        Next x
        
        If r > 0 Then
            outrange.Offset(-1).Resize(1, c) = Range("C5:I5").Value
            outrange.Resize(65000, c) = Output
        End If
            
    End Sub
    Values in red would need to be changed if you ever rearrange your sheet.
    Wow, I appreciate your kind help Eric . It worked like an appeal. Absolutely Solved!

    Good Luck


    Kind Regards,

    Moti


    Eric, I have one more question how can I list of all (min+sum, max+sum), (min-sum and min-sum) then generate one by one with all set to know how much could be total with each sum = sum must be 4.782.969


    Max+sum = 56 with this there is only 1 set

    Min+sum = -113 with this there is only 1 set


    ABCDEFGHIJ
    1
    2
    3
    4
    5n1n2n3n4n5n6n7
    6-4 8 1 4 -3 8 8
    72 -6 5 -2 6 -2 -4
    87 1 3 8 8 -5 5
    94 -2 8 3 2 -3 -2
    100 4 7 6 -10 6 4
    113 6 -32 1 -1 -8 2
    126 2 2 -16 7 3 6
    138 7 -16 7 3 7 -9
    14-2 -18 4 -32 5 2 -3
    15
    16Sum
    17Min+ sum211122211
    18Max+ Sum888888856
    19
    20Min- Sum-4-18-32-32-10-8-9-113
    21Max-Sum-2-2-16-2-1-2-2-27

    Sheet1





    Kind Regards,

    Moti

    Last edited by motilulla; Sep 11th, 2019 at 05:21 PM.

  9. #9
    MrExcel MVP Eric W's Avatar
    Join Date
    Aug 2015
    Location
    Bountiful, UT
    Posts
    8,628
    Post Thanks / Like
    Mentioned
    42 Post(s)
    Tagged
    5 Thread(s)

    Default Re: VBA get target sum from the grid of 7*9

    I'm not entirely sure what you want, but try:

    Code:
    Sub Sub97c()
    Dim r As Long, c As Long, Dict As Object, grid As Variant, ix() As Long, outrange As Range
    Dim s As Long, sc As String, target As Long, output(), x As Variant, y As Variant
    
    
        Set Dict = CreateObject("Scripting.Dictionary")
        
        target = Range("S6").Value
        grid = Range("C6:I14")
        Set outrange = Range("B25")
        
        r = UBound(grid, 1)
        c = UBound(grid, 2)
        ReDim ix(1 To c)
        
        For i = -113 To 56
            Dict(i) = 0
        Next i
        For i = 1 To c
            ix(i) = 1
        Next i
        
    ChkAgain:
        s = 0
        sc = ""
        For i = 1 To c
            s = s + grid(ix(i), i)
            sc = sc & grid(ix(i), i) & "|"
        Next i
        Dict(s) = Dict(s) + 1
        
        For i = 1 To c
            ix(i) = ix(i) + 1
            If ix(i) <= r Then GoTo ChkAgain:
            ix(i) = 1
        Next i
        
        outrange.Resize(1, 2) = Array("Value", "Number of ways to achieve value")
        outrange.Offset(1).Resize(Dict.Count) = WorksheetFunction.Transpose(Dict.keys)
        outrange.Offset(1, 1).Resize(Dict.Count) = WorksheetFunction.Transpose(Dict.items)
            
    End Sub
    Change the B25 to where you want the output table to go.
    Cheers,
    Eric

    When you eliminate the impossible, whatever remains, however improbable, must be the truth.

    -Posting guidelines, forum rules, terms of use, FAQs, BB codes, See how to search the forum
    -Post a screen shot with the HTML Maker

  10. #10
    Board Regular
    Join Date
    Feb 2008
    Posts
    1,752
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA get target sum from the grid of 7*9

    Quote Originally Posted by Eric W View Post
    I'm not entirely sure what you want, but try:

    Code:
    Sub Sub97c()
    Dim r As Long, c As Long, Dict As Object, grid As Variant, ix() As Long, outrange As Range
    Dim s As Long, sc As String, target As Long, output(), x As Variant, y As Variant
    
    
        Set Dict = CreateObject("Scripting.Dictionary")
        
        target = Range("S6").Value
        grid = Range("C6:I14")
        Set outrange = Range("B25")
        
        r = UBound(grid, 1)
        c = UBound(grid, 2)
        ReDim ix(1 To c)
        
        For i = -113 To 56
            Dict(i) = 0
        Next i
        For i = 1 To c
            ix(i) = 1
        Next i
        
    ChkAgain:
        s = 0
        sc = ""
        For i = 1 To c
            s = s + grid(ix(i), i)
            sc = sc & grid(ix(i), i) & "|"
        Next i
        Dict(s) = Dict(s) + 1
        
        For i = 1 To c
            ix(i) = ix(i) + 1
            If ix(i) <= r Then GoTo ChkAgain:
            ix(i) = 1
        Next i
        
        outrange.Resize(1, 2) = Array("Value", "Number of ways to achieve value")
        outrange.Offset(1).Resize(Dict.Count) = WorksheetFunction.Transpose(Dict.keys)
        outrange.Offset(1, 1).Resize(Dict.Count) = WorksheetFunction.Transpose(Dict.items)
            
    End Sub
    Change the B25 to where you want the output table to go.
    Eric, I just ask for list but have given an entire solution with all the ended combinations. Surprised also within a 15 min that is unbelievable. I am very great full to you Eric it has been time saver solution for me.

    Thank you for solving complete request. Eric I am


    Have a good day and Good Luck


    Kind Regards,

    Moti


Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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