Make combinations until a require sum is reached...

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,422
Office Version
  1. 2010
Hello,

I am looking a VBA collect the 6 number from the 6 rows which sums reach 15 in this given example (but if it is possible to make VBA flexible so can be chosen any sum)

Example1 picks 5 numbers from (row 3,4,5,6 &7 from 5 rows) from cells B3:B7 which sums =13 then check row 8 from B8:J8 adding each number with row (with cells B3:B7 total) if sum target 15 match list them in L2:Q2 as B3:B7 Total is 13 there is no number 2 in row 8 so no sum match found =15

Check this example1 below


Book1
ABCDEFGHIJKLMNOPQ
1
2n1n2n3n4n5n6n7n8n9R1R2R3R4R5R6
304921562322
44117693502
50231542101611
673480214913
72014320116105
81201355314176
Hoja7


Example2 picks 4 numbers from (row 3,4,5 & 6 from 4 rows) from cells B3:B6 an one next number from row7 = 2 now sums are =11 then check again with row 8 from B8:J8 adding each number with row (with cells B3:B6 +C7 total) if sum target 15 match as B3:B6 +C7Total is 11 there is no number 4 in row 8 so no sum match found =15


Book1
ABCDEFGHIJKLMNOPQ
1
2n1n2n3n4n5n6n7n8n9R1R2R3R4R5R6
304921562322
44117693502
50231542101611
673480214913
72014320116105
81201355314176
Hoja7


Example3 repeat the step above pick next number in row 7 = 14 now 11+14 =25 no match 15 found


Book1
ABCDEFGHIJKLMNOPQ
1
2n1n2n3n4n5n6n7n8n9R1R2R3R4R5R6
304921562322
44117693502
50231542101611
673480214913
72014320116105
81201355314176
Hoja7


Example4 repeat the step above pick next number in row 7 = 3 now 11+3 =14 and in row 8 found number 1 so 14+1 = 15 match found list it in L2:Q2


Book1
ABCDEFGHIJKLMNOPQ
1
2n1n2n3n4n5n6n7n8n9R1R2R3R4R5R6
304921562322040731
44117693502
50231542101611
673480214913
72014320116105
81201355314176
Hoja7


Example5 repeat the step above pick next number in row 7 = 20 now 11+20 =31 no match 15 found


Book1
ABCDEFGHIJKLMNOPQ
1
2n1n2n3n4n5n6n7n8n9R1R2R3R4R5R6
304921562322040731
44117693502
50231542101611
673480214913
72014320116105
81201355314176
Hoja7


Example6 repeat the step above pick next number in row 7 = 1 now 11+1 =12 and in row 8 found number 3 so 12+3 = 15 match found list it in L3:Q3


Book1
ABCDEFGHIJKLMNOPQ
1
2n1n2n3n4n5n6n7n8n9R1R2R3R4R5R6
304921562322040731
44117693502040713
50231542101611
673480214913
72014320116105
81201355314176
Hoja7


Here are some more matches but not all


Book1
ABCDEFGHIJKLMNOPQ
1
2n1n2n3n4n5n6n7n8n9R1R2R3R4R5R6
304921562322040731
44117693502040713
50231542101611040335
673480214913040353
72014320116105040425
81201355314176040416
9040821
10040803
11040056
12040236
13040155
14042711
15042333
16042423
17042036
18042225
Hoja7


Please need VBA solution to get all combinations

Thank you all

Excel 2000
Regards,
Moti
 
Hi Moti,
new code calculates max sum, this way there is no need to hardcode it.
Code:
Sub moti_11_2016_OnlyReport1Pass2()
Dim x1 As Long: Dim x2 As Long: Dim x3 As Long
Dim x4 As Long: Dim x5 As Long: Dim x6 As Long
Dim mynumbers As Variant
Dim check As Long
Dim x As Long
Dim maxsum As Long
Dim check1 As Variant

'calculates max sum
[COLOR=#ff0000]maxsum = WorksheetFunction.Max(Range("B3:J3")) + WorksheetFunction.Max(Range("B4:J4")) + WorksheetFunction.Max(Range("B5:J5")) + _
        WorksheetFunction.Max(Range("B6:J6")) + WorksheetFunction.Max(Range("B7:J7")) + WorksheetFunction.Max(Range("B8:J8"))
[/COLOR]'
ReDim check1(0 To maxsum, 1 To 2)
For x = 0 To UBound(check1, 1)
    check1(x, 1) = x
    check1(x, 2) = 0
Next x
mynumbers = Range("B3:J8").Value
For x1 = 1 To 9: For x2 = 1 To 9: For x3 = 1 To 9: For x4 = 1 To 9: For x5 = 1 To 9: For x6 = 1 To 9
    check = (mynumbers(1, x1) + mynumbers(2, x2) + mynumbers(3, x3) + mynumbers(4, x4) + mynumbers(5, x5) + mynumbers(6, x6)) ' = check Then
    check1(check, 2) = check1(check, 2) + 1
Next x6: Next x5: Next x4: Next x3: Next x2: Next x1
Range("[COLOR=#ff0000]Q[/COLOR]3:[COLOR=#ff0000]R[/COLOR]" & (maxsum + 3)) = check1 'edit columns to change output layout
MsgBox ("E N D")
End Sub
if you want to change columns output to Y:Z change this line
Code:
Range("[COLOR=#ff0000]Q[/COLOR]3:[COLOR=#ff0000]R[/COLOR]" & (maxsum + 3)) = check1
to
Code:
Range("[COLOR=#ff0000]Y[/COLOR]3:[COLOR=#ff0000]Z[/COLOR]" & (maxsum + 3)) = check1
Have a nice rest of the day
 
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi Moti,
new code calculates max sum, this way there is no need to hardcode it.
Code:
Sub moti_11_2016_OnlyReport1Pass2()
Dim x1 As Long: Dim x2 As Long: Dim x3 As Long
Dim x4 As Long: Dim x5 As Long: Dim x6 As Long
Dim mynumbers As Variant
Dim check As Long
Dim x As Long
Dim maxsum As Long
Dim check1 As Variant

'calculates max sum
[COLOR=#ff0000]maxsum = WorksheetFunction.Max(Range("B3:J3")) + WorksheetFunction.Max(Range("B4:J4")) + WorksheetFunction.Max(Range("B5:J5")) + _
        WorksheetFunction.Max(Range("B6:J6")) + WorksheetFunction.Max(Range("B7:J7")) + WorksheetFunction.Max(Range("B8:J8"))
[/COLOR]'
ReDim check1(0 To maxsum, 1 To 2)
For x = 0 To UBound(check1, 1)
    check1(x, 1) = x
    check1(x, 2) = 0
Next x
mynumbers = Range("B3:J8").Value
For x1 = 1 To 9: For x2 = 1 To 9: For x3 = 1 To 9: For x4 = 1 To 9: For x5 = 1 To 9: For x6 = 1 To 9
    check = (mynumbers(1, x1) + mynumbers(2, x2) + mynumbers(3, x3) + mynumbers(4, x4) + mynumbers(5, x5) + mynumbers(6, x6)) ' = check Then
    check1(check, 2) = check1(check, 2) + 1
Next x6: Next x5: Next x4: Next x3: Next x2: Next x1
Range("[COLOR=#ff0000]Q[/COLOR]3:[COLOR=#ff0000]R[/COLOR]" & (maxsum + 3)) = check1 'edit columns to change output layout
MsgBox ("E N D")
End Sub
if you want to change columns output to Y:Z change this line
Code:
Range("[COLOR=#ff0000]Q[/COLOR]3:[COLOR=#ff0000]R[/COLOR]" & (maxsum + 3)) = check1
to
Code:
Range("[COLOR=#ff0000]Y[/COLOR]3:[COLOR=#ff0000]Z[/COLOR]" & (maxsum + 3)) = check1
Have a nice rest of the day
Hello B___P, clever code now it is working with any max sum no problem thumbs up!! Also I appreciate for making ease the shift of result columns.

Many thanks to you for a great gob and for the kind help you gave me

Have a nice weekend

Kind Regards,
Moti :)
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,189
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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