VBA could build a combination with the set of 14 numbers

Kishan

Well-known Member
Joined
Mar 15, 2011
Messages
1,648
Office Version
  1. 2010
Platform
  1. Windows
Using Excel 2000

Hi,

I got table, which got 3 groups of 14 numbers & 14 sets each with 3 numbers
Building combinations in the set of 14 numbers it could make with min to max sum, which is in the range of 287 to 315 are listed in the cells U5:V33

3 groups - each group contain 14 numbers
Group-1 Find in the range D5:Q5
Group-2 Find in the range D6:Q6
Group-3 Find in the range D7:Q7

14 set - each set contain 3 numbers
Set-1 Find in the range D5:D7
Set-2 Find in the range E5:E7
Set-3 Find in the range F5:F7
Set-4 Find in the range G5:G7
Set-5 Find in the range H5:H7
Set-6 Find in the range I5:I7
Set-7 Find in the range J5:J7
Set-8 Find in the range K5:K7
Set-9 Find in the range L5:L7
Set-10 Find in the range M5:M7
Set-11 Find in the range N5:N7
Set-12 Find in the range O5:O7
Set-13 Find in the range P5:P7
Set-14 Find in the range Q5:Q7

Method of building combinations:
Pick numbers from any group but only 1 from the each set
For example...
If first chosen num is 1 (num-1 is from the 1st-set, so 2 & 3 can not be chosen as second num)
Second num must be chosen from the 2nd set if the second num is chosen 5 (num-5 is from the 2nd-set, so 4 & 6 can not be chosen as third num) and so on.... to build each combinations with set of 14 numbers.

My query is it possible to get VBA, which can generate combinations with desire SUM
For example...I want to get all combinations with SUM 293 that can be total 19.383
As per my layout if I set target sum in the cell C2 = 293 can I get all 19.383 combinations in the range D12:Q19394

I have generated manually 36 combinations just to show as the example data in the range D12:Q47


Book1
ABCDEFGHIJKLMNOPQRSTUV
1Target Sum
2293
3Min & MaxCombinationsTotal
4Set-1Set-2Set-3Set-4Set-5Set-6Set-7Set-8Set-9Set-10Set-11Set-12Set-13Set-14SUMOf SumCombinations
5Group-114710131619222528313437402872871
6Group-2258111417202326293235384128814
7Group-33691215182124273033363942315289105
8290546
92912.184
102927.098
11CombinationsSet-1Set-2Set-3Set-4Set-5Set-6Set-7Set-8Set-9Set-10Set-11Set-12Set-13Set-14Sum29319.383
121357101417192225283134384029329445.474
132247101316202325293234374129329593.093
1431591013161923253031343740293296168.168
1541671114161922252831343742293297270.270
1651471213161922252833363740293298388.752
1761471013162022252931363940293299502.593
1872691013162022252831343740293300585.690
1982481013161922252833343841293301616.227
2092671213161922252831353740293302585.690
21101471013161923252931343942293303502.593
22111571214161922252832353740293304388.752
23123571213171922252831343740293305270.270
24131471014172122262832343740293306168.168
2514167101317202325283134374129330793.093
2615247101417192226283134374229330845.474
2716147101318192225283235374229330919.383
281734711131619232628313537402933107.098
291824710131619222729333437402933112.184
30192481014171922252931343741293312546
31201471014162124252931343740293313105
3221148101416192226283234394029331414
332215710151719222529313438402933151
34231491014161923252832343840293
35241571014162123252831343741293
36251471013171923252832353742293
37261481213172022252832343740293
38271471213161922272931353740293
39281471013182022252832353741293
40293471213181922252831343740293
41303471013162122252832343741293
42311471113171922262832343742293
43321491013181922252831343940293
44333471213161922262832343740293
45341491113161922253031343840293
46351471114161924252832343840293
47363481013161923252832343840293
Sheet1


Thank you in advance

Kishan
 
Last edited:

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
I still submit that there's no benefit to enumerating all the combinations, but if you really want the list, here's a macro. It requires your sheet set up as shown, C2 has the target sum, and the totals in U:V are required.

Code:
Public Sub ListCombs()
Dim ix(1 To 14) As Long, i As Long, target As Long, ulim As Long, ctr As Long, Results()

    target = Range("C2").Value
    ulim = WorksheetFunction.VLookup(target, Range("U5:V33"), 2)
    Range("C12:S" & 11 + ulim).ClearContents
    ReDim Results(1 To ulim, 1 To 17)
    ctr = 0
    
    For i = 1 To 14
        ix(i) = (i - 1) * 3 + 1
    Next i
    
MyLoop:
    If WorksheetFunction.Sum(ix) = target Then
        ctr = ctr + 1
        For i = 1 To 14
            Results(ctr, i + 1) = ix(i)
        Next i
        Results(ctr, 1) = ctr
        Results(ctr, 17) = target
        If ctr = ulim Then GoTo ImDone:
    End If
        
    For i = 1 To 14
        ix(i) = ix(i) + 1
        If ix(i) Mod 3 <> 1 Then Exit For
        ix(i) = ix(i) - 3
    Next i
    If i < 15 Then GoTo MyLoop:


ImDone:
    Range("C12:S" & 11 + ulim).Value = Results


End Sub
 
Upvote 0
I still submit that there's no benefit to enumerating all the combinations, but if you really want the list, here's a macro. It requires your sheet set up as shown, C2 has the target sum, and the totals in U:V are required.
Wow Eric, speechless thank you so much for giving a great solution :) it is spot on and fast too.

Please one more request as I am using very older version 2000 that has row limit 65536

As there are max 616.227 combinations with sum 301 to get listed all them, it need to add 9 more sheets or may be combinations divide in 65000
First set of 65000 start writing in columns AA12:AN64988, Second set of 65000 start writing in columns AP12:BC64988

I mean 14 columns then next + 1 & 14 and so on...

Is it possible Please?

Thank you

Good Luck

Regards,
Kishan :)
 
Upvote 0
Try:

Code:
Public Sub ListCombs()
Dim ix(1 To 14) As Long, i As Long, target As Long, ctr As Long, Results(1 To 65000, 1 To 17)
Dim PageNo As Long, c2 As Long


    Application.ScreenUpdating = False
    target = Range("C2").Value
    ctr = 0
    
    For i = 1 To 14
        ix(i) = (i - 1) * 3 + 1
    Next i
    
MyLoop:
    If WorksheetFunction.Sum(ix) = target Then
        ctr = ctr + 1
        c2 = ((ctr - 1) Mod 65000) + 1
        For i = 1 To 14
            Results(c2, i + 1) = ix(i)
        Next i
        Results(c2, 1) = ctr
        Results(c2, 17) = target
        If ctr Mod 65000 = 0 Then
            PageNo = PageNo + 1
            With Worksheets.Add
                .Range("A1:Q1").Value = Array("Counter", "Grp1", "Grp2", "Grp3", "Grp4", "Grp5", "Grp6", "Grp7", _
                                       "Grp8", "Grp9", "Grp10", "Grp11", "Grp12", "Grp13", "Grp14", "", "")
                .Cells(1, 17) = "Page " & PageNo
                .Range("A2:Q65001").Value = Results
            End With
            Erase Results
        End If
            
    End If
        
    For i = 1 To 14
        ix(i) = ix(i) + 1
        If ix(i) Mod 3 <> 1 Then Exit For
        ix(i) = ix(i) - 3
    Next i
    If i < 15 Then GoTo MyLoop:


    PageNo = PageNo + 1
    With Worksheets.Add
        .Range("A1:Q1").Value = Array("Counter", "Grp1", "Grp2", "Grp3", "Grp4", "Grp5", "Grp6", "Grp7", _
                                       "Grp8", "Grp9", "Grp10", "Grp11", "Grp12", "Grp13", "Grp14", "", "")
        .Cells(1, 17) = "Page " & PageNo
        .Range("A2:Q65001").Value = Results
    End With
    
    Application.ScreenUpdating = True


End Sub
Start it while your main page is visible, and put your target sum in C2.
 
Upvote 0
Try:

Code:
Public Sub ListCombs()
Dim ix(1 To 14) As Long, i As Long, target As Long, ctr As Long, Results(1 To 65000, 1 To 17)
Dim PageNo As Long, c2 As Long


    Application.ScreenUpdating = False
    target = Range("C2").Value
    ctr = 0
    
    For i = 1 To 14
        ix(i) = (i - 1) * 3 + 1
    Next i
    
MyLoop:
    If WorksheetFunction.Sum(ix) = target Then
        ctr = ctr + 1
        c2 = ((ctr - 1) Mod 65000) + 1
        For i = 1 To 14
            Results(c2, i + 1) = ix(i)
        Next i
        Results(c2, 1) = ctr
        Results(c2, 17) = target
        If ctr Mod 65000 = 0 Then
            PageNo = PageNo + 1
            With Worksheets.Add
                .Range("A1:Q1").Value = Array("Counter", "Grp1", "Grp2", "Grp3", "Grp4", "Grp5", "Grp6", "Grp7", _
                                       "Grp8", "Grp9", "Grp10", "Grp11", "Grp12", "Grp13", "Grp14", "", "")
                .Cells(1, 17) = "Page " & PageNo
                .Range("A2:Q65001").Value = Results
            End With
            Erase Results
        End If
            
    End If
        
    For i = 1 To 14
        ix(i) = ix(i) + 1
        If ix(i) Mod 3 <> 1 Then Exit For
        ix(i) = ix(i) - 3
    Next i
    If i < 15 Then GoTo MyLoop:


    PageNo = PageNo + 1
    With Worksheets.Add
        .Range("A1:Q1").Value = Array("Counter", "Grp1", "Grp2", "Grp3", "Grp4", "Grp5", "Grp6", "Grp7", _
                                       "Grp8", "Grp9", "Grp10", "Grp11", "Grp12", "Grp13", "Grp14", "", "")
        .Cells(1, 17) = "Page " & PageNo
        .Range("A2:Q65001").Value = Results
    End With
    
    Application.ScreenUpdating = True


End Sub
Start it while your main page is visible, and put your target sum in C2.
Amazing! Eric W, You took a step ahead of my thought very well set-up, every page has header clearly informed by counter, group & including the page numbers.

code is a very speedy I tried with max sum 301 which makes 616.227 combinations code create 9 sheets each with 65000*9 =585.000 & the 10th one with the rest 31.227 It took just a seconds as quick as eye blink

I cannot thank you enough.
It is a wow! Code

Good Luck

Have a great weekend

Kind Regards
Kishan :)

 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,246
Members
452,623
Latest member
cliftonhandyman

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