Conditional combination's pick only "1" number from each column in the row

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,361
Office Version
  1. 2010
Conditional combination's pick only "1" number from each column in the row <o:p></o:p>
<o:p></o:p>
Hello,<o:p></o:p>
<o:p></o:p>
I have some numbers in column D:J (each column has variable quantity of numbers in it) <o:p></o:p>
In the column D only 1 number which is 2 (this is like a joker will be used with all the columns numbers to create combinations) <o:p></o:p>
In the column E, G, H, I, J there are 2 numbers and in column F there are 4 numbers<o:p></o:p>
<o:p></o:p>
Require VBA which could make all Conditional combination's picking only "1" number from each column in the row (Conditional combination's) I mean for example in the row can not be used 2 number from any columns, 2 is a common number which will be combined with all column number to create all combinations<o:p></o:p>
<o:p></o:p>
Some example rows are Conditional combination's listed below in the column N:U <o:p></o:p>


Book1
ABCDEFGHIJKLMNOPQRSTU
1
2
3n1n2n3n4n5n6n7R1R2R3R4R5R6R7Sum
42210842221084219
56262318261084223
63221684225
792210234234
8221081216
9221084825
10262084224
11261684229
122610234238
13261084829
14222684226
152220234235
16222081217
17222084826
Sheet10
<o:p></o:p>
<o:p></o:p>
Thank you all<o:p></o:p>
<o:p></o:p>
Excel 2000<o:p></o:p>
Regards,
Moti
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hello, to make simpler my request there are couples of things</SPAN></SPAN>

1- Cell D4 number 2 is a number, which has to be used in each row with all the combinations, rest 6 numbers need to combine with it to generate all combinations
</SPAN></SPAN>
2- in the each row of combinations should not be taken 2 or more numbers from any of the 6 columns say for example 2-
2-6-1-0-8-4, (2-6 can not be taken from E4 & E5) in the same row to create any combinations</SPAN></SPAN>

Hope this makes bit clearer
</SPAN></SPAN>

Regards,
</SPAN>
Moti
</SPAN>
 
Upvote 0
Hello, to make simpler my request there are couples of things

1- Cell D4 number 2 is a number, which has to be used in each row with all the combinations, rest 6 numbers need to combine with it to generate all combinations

2- in the each row of combinations should not be taken 2 or more numbers from any of the 6 columns say for example 2-
2-6-1-0-8-4, (2-6 can not be taken from E4 & E5) in the same row to create any combinations

Hope this makes bit clearer


Regards,

Moti


Try this:

Code:
Sub a1080399a()
'https://www.mrexcel.com/forum/excel-questions/1080399-conditional-combinations-pick-only-1-number-each-column-row.html
Dim a As Long, b As Long, c As Long, d As Long, e As Long, f As Long, n As Long
Dim va, vb, vc, vd, ve, vf, vx

va = Range("E4", Cells(Rows.count, "E").End(xlUp))
vb = Range("F4", Cells(Rows.count, "F").End(xlUp))
vc = Range("G4", Cells(Rows.count, "G").End(xlUp))
vd = Range("H4", Cells(Rows.count, "H").End(xlUp))
ve = Range("I4", Cells(Rows.count, "I").End(xlUp))
vf = Range("J4", Cells(Rows.count, "J").End(xlUp))

ReDim vx(1 To 1000, 1 To 7)

For a = 1 To UBound(va)
    For b = 1 To UBound(vb)
        For c = 1 To UBound(vc)
            For d = 1 To UBound(vd)
                For e = 1 To UBound(ve)
                    For f = 1 To UBound(vf)
                    
                        n = n + 1
                        vx(n, 1) = 2
                        vx(n, 2) = va(a, 1)
                        vx(n, 3) = vb(b, 1)
                        vx(n, 4) = vc(c, 1)
                        vx(n, 5) = vd(d, 1)
                        vx(n, 6) = ve(e, 1)
                        vx(n, 7) = vf(f, 1)

                    Next
                Next
            Next
        Next
    Next
Next

Range("N4").Resize(n, 7) = vx

End Sub
 
Upvote 0
Try this:

Code:
Sub a1080399a()
'https://www.mrexcel.com/forum/excel-questions/1080399-conditional-combinations-pick-only-1-number-each-column-row.html
Dim a As Long, b As Long, c As Long, d As Long, e As Long, f As Long, n As Long
Dim va, vb, vc, vd, ve, vf, vx

va = Range("E4", Cells(Rows.count, "E").End(xlUp))
vb = Range("F4", Cells(Rows.count, "F").End(xlUp))
vc = Range("G4", Cells(Rows.count, "G").End(xlUp))
vd = Range("H4", Cells(Rows.count, "H").End(xlUp))
ve = Range("I4", Cells(Rows.count, "I").End(xlUp))
vf = Range("J4", Cells(Rows.count, "J").End(xlUp))

ReDim vx(1 To 1000, 1 To 7)

For a = 1 To UBound(va)
    For b = 1 To UBound(vb)
        For c = 1 To UBound(vc)
            For d = 1 To UBound(vd)
                For e = 1 To UBound(ve)
                    For f = 1 To UBound(vf)
                    
                        n = n + 1
                        vx(n, 1) = 2
                        vx(n, 2) = va(a, 1)
                        vx(n, 3) = vb(b, 1)
                        vx(n, 4) = vc(c, 1)
                        vx(n, 5) = vd(d, 1)
                        vx(n, 6) = ve(e, 1)
                        vx(n, 7) = vf(f, 1)

                    Next
                Next
            Next
        Next
    Next
Next

Range("N4").Resize(n, 7) = vx

End Sub
Akuini, VBA work superb!! (y) I imagine this were difficult task.

I did mention 1st column D I would be use only the 1 number, please is it possible for you to alter the code if I use either 1 or 2 numbers in the D?
</SPAN></SPAN>

Thank you so much for your kind help.
</SPAN></SPAN>

Have a nice week ahead
</SPAN></SPAN>

Kind Regards
</SPAN></SPAN>
Moti :biggrin:
</SPAN></SPAN>
 
Upvote 0
Akuini, VBA work superb!! (y) I imagine this were difficult task.

I did mention 1st column D I would be use only the 1 number, please is it possible for you to alter the code if I use either 1 or 2 numbers in the D?


Thank you so much for your kind help.


Have a nice week ahead


Kind Regards

Moti :biggrin:

Ok, try this:

Code:
Sub a1080399b()
'https://www.mrexcel.com/forum/excel-questions/1080399-conditional-combinations-pick-only-1-number-each-column-row.html
Dim a As Long, b As Long, c As Long, d As Long, e As Long, f As Long, n As Long
Dim va, vb, vc, vd, ve, vf, vx, vz

vz = Range("D4:D5")
va = Range("E4", Cells(Rows.count, "E").End(xlUp))
vb = Range("F4", Cells(Rows.count, "F").End(xlUp))
vc = Range("G4", Cells(Rows.count, "G").End(xlUp))
vd = Range("H4", Cells(Rows.count, "H").End(xlUp))
ve = Range("I4", Cells(Rows.count, "I").End(xlUp))
vf = Range("J4", Cells(Rows.count, "J").End(xlUp))

ReDim vx(1 To 1000, 1 To 7)

For z = 1 To UBound(vz)
If vz(z, 1) = "" Then Exit For
    For a = 1 To UBound(va)
        For b = 1 To UBound(vb)
            For c = 1 To UBound(vc)
                For d = 1 To UBound(vd)
                    For e = 1 To UBound(ve)
                        For f = 1 To UBound(vf)
                        
                            n = n + 1
                            vx(n, 1) = vz(z, 1)
                            vx(n, 2) = va(a, 1)
                            vx(n, 3) = vb(b, 1)
                            vx(n, 4) = vc(c, 1)
                            vx(n, 5) = vd(d, 1)
                            vx(n, 6) = ve(e, 1)
                            vx(n, 7) = vf(f, 1)
    
                        Next
                    Next
                Next
            Next
        Next
    Next
Next

Range("N4").Resize(n, 7) = vx

End Sub
 
Upvote 0
Ok, try this:

Code:
Sub a1080399b()
'https://www.mrexcel.com/forum/excel-questions/1080399-conditional-combinations-pick-only-1-number-each-column-row.html
Dim a As Long, b As Long, c As Long, d As Long, e As Long, f As Long, n As Long
Dim va, vb, vc, vd, ve, vf, vx, vz

vz = Range("D4:D5")
va = Range("E4", Cells(Rows.count, "E").End(xlUp))
vb = Range("F4", Cells(Rows.count, "F").End(xlUp))
vc = Range("G4", Cells(Rows.count, "G").End(xlUp))
vd = Range("H4", Cells(Rows.count, "H").End(xlUp))
ve = Range("I4", Cells(Rows.count, "I").End(xlUp))
vf = Range("J4", Cells(Rows.count, "J").End(xlUp))

ReDim vx(1 To 1000, 1 To 7)

For z = 1 To UBound(vz)
If vz(z, 1) = "" Then Exit For
    For a = 1 To UBound(va)
        For b = 1 To UBound(vb)
            For c = 1 To UBound(vc)
                For d = 1 To UBound(vd)
                    For e = 1 To UBound(ve)
                        For f = 1 To UBound(vf)
                        
                            n = n + 1
                            vx(n, 1) = vz(z, 1)
                            vx(n, 2) = va(a, 1)
                            vx(n, 3) = vb(b, 1)
                            vx(n, 4) = vc(c, 1)
                            vx(n, 5) = vd(d, 1)
                            vx(n, 6) = ve(e, 1)
                            vx(n, 7) = vf(f, 1)
    
                        Next
                    Next
                Next
            Next
        Next
    Next
Next

Range("N4").Resize(n, 7) = vx

End Sub
Akuini, Thank you so much for modifying the code as per my second request. It is working flawless!!</SPAN></SPAN>

Kind Regards
</SPAN></SPAN>
Moti
:) </SPAN></SPAN>
 
Upvote 0
Akuini, Thank you so much for modifying the code as per my second request. It is working flawless!!</SPAN></SPAN>

Kind Regards
</SPAN></SPAN>
Moti
:) </SPAN></SPAN>
Akuini, I understand it is irritating for you or any programmer is doing voluntarily to solve ones problems, in this thread you solved twice. After getting twice solutions by you and working on it I find if this finally could be the done the way which I did not thought till having code by you and getting the result of my analysis.</SPAN></SPAN>

Before the code I thought that having the 9 numbers in each of the 7 columns, it could be approximately 20000 to 25000 rows combinations. I modified 2 thing in code from post#5, 1- change the range Range("D4:D5") to Range("D4:D12") and ReDim vx(1 To 1000, 1 To 7) to ReDim vx(1 To 100000, 1 To 7) and filled 9 number in the each column in cells D4:J12.
</SPAN></SPAN>

After changes when I run the code it stop at the line--> vx(n, 1) = vz(z, 1) I imagine this is because there are more than 65536 row which my excel 2000 does not permit. It is not a code problem.
</SPAN></SPAN>

There could be the 2 solutions one code continuing writing in the next columns or other options restrict the sum.
</SPAN></SPAN>

Akuini, this is my third request in the same thread. Please will it be possible for you to recoding to get result from the following data. Minimum sum will be 0 and max sum can be 162, as using all these number there could be more than 65000 combinations.
</SPAN></SPAN>

It is just my thoughts if the code generates all combinations rows has the fix sum example 36 it would be limited or if possible it could generate in the sum between range say for example 28 to 38. And these min & max sum values can be filled in cell B1 & B2 before the code execute. I think will be, always less then 65000 rows of combinations
</SPAN></SPAN>

Here is the new example data
</SPAN></SPAN>

Book1
ABCDEFGHIJKLMNOPQRSTU
1Min28Max Each Col2712942231633162
2Max38
3n1n2n3n4n5n6n7R1R2R3R4R5R6R7Sum
41211143
50620812
62124618108
7275742360
81248223146
9630269717
1031312601
111199112933
129054001619
13
14
15
16
17
Sheet10


Sorry for the troubles
</SPAN></SPAN>

Thank you
</SPAN></SPAN>

Kind Regards
</SPAN></SPAN>
Moti
</SPAN></SPAN>
 
Last edited:
Upvote 0

Akuini, this is my third request in the same thread. Please will it be possible for you to recoding to get result from the following data. Minimum sum will be 0 and max sum can be 162, as using all these number there could be more than 65000 combinations.


It is just my thoughts if the code generates all combinations rows has the fix sum example 36 it would be limited or if possible it could generate in the sum between range say for example 28 to 38. And these min & max sum values can be filled in cell B1 & B2 before the code execute. I think will be, always less then 65000 rows of combinations


Moti



Ok, try this:
The macro will stop if the result reach 65000 rows. Try min = max first, see what you get.


Code:
Sub a1080399c()
'https://www.mrexcel.com/forum/excel-questions/1080399-conditional-combinations-pick-only-1-number-each-column-row.html
Dim a As Long, b As Long, c As Long, d As Long, e As Long, f As Long, n As Long
Dim va, vb, vc, vd, ve, vf, vx

vz = Range("D4:D12")
va = Range("E4", Cells(Rows.count, "E").End(xlUp))
vb = Range("F4", Cells(Rows.count, "F").End(xlUp))
vc = Range("G4", Cells(Rows.count, "G").End(xlUp))
vd = Range("H4", Cells(Rows.count, "H").End(xlUp))
ve = Range("I4", Cells(Rows.count, "I").End(xlUp))
vf = Range("J4", Cells(Rows.count, "J").End(xlUp))


mn = Range("B1")
mx = Range("B2")

ax = 65000
ReDim vx(1 To ax, 1 To 7)

For z = 1 To UBound(vz)
If vz(z, 1) = "" Then Exit For
    For a = 1 To UBound(va)
        For b = 1 To UBound(vb)
            For c = 1 To UBound(vc)
                For d = 1 To UBound(vd)
                    For e = 1 To UBound(ve)
                        For f = 1 To UBound(vf)
                        dSum = vz(z, 1) + va(a, 1) + vb(b, 1) + vc(c, 1) + vd(d, 1) + ve(e, 1) + vf(f, 1)
                        If dSum >= mn And dSum <= mx Then
                        'If dSum = mn Then
                            n = n + 1
                            vx(n, 1) = vz(z, 1)
                            vx(n, 2) = va(a, 1)
                            vx(n, 3) = vb(b, 1)
                            vx(n, 4) = vc(c, 1)
                            vx(n, 5) = vd(d, 1)
                            vx(n, 6) = ve(e, 1)
                            vx(n, 7) = vf(f, 1)
                        
                        If n >= ax Then
                        MsgBox "The result is more than " & ax & " rows. This macro will stop"
                        Exit Sub
                        End If
                        
                        End If
                        Next
                    Next
                Next
            Next
        Next
    Next
Next

Range("N4").Resize(n, 7) = vx

End Sub


Using your example, all combination will reach 9^7 = 4,782,969 rows & 7 columns.
A worksheet in Excel 2000 is limited to 65,536 rows & 256 column. If you want to get all the 4,782,969 rows x 7 columns, there's a workaround. We can put the result per 65000 rows, and maybe we can use about 250 column & we may need 3 worksheets.
Let me know if this approach suit you.
 
Upvote 0
Ok, try this:
The macro will stop if the result reach 65000 rows. Try min = max first, see what you get.


Code:
Sub a1080399c()
'https://www.mrexcel.com/forum/excel-questions/1080399-conditional-combinations-pick-only-1-number-each-column-row.html
Dim a As Long, b As Long, c As Long, d As Long, e As Long, f As Long, n As Long
Dim va, vb, vc, vd, ve, vf, vx

vz = Range("D4:D12")
va = Range("E4", Cells(Rows.count, "E").End(xlUp))
vb = Range("F4", Cells(Rows.count, "F").End(xlUp))
vc = Range("G4", Cells(Rows.count, "G").End(xlUp))
vd = Range("H4", Cells(Rows.count, "H").End(xlUp))
ve = Range("I4", Cells(Rows.count, "I").End(xlUp))
vf = Range("J4", Cells(Rows.count, "J").End(xlUp))


mn = Range("B1")
mx = Range("B2")

ax = 65000
ReDim vx(1 To ax, 1 To 7)

For z = 1 To UBound(vz)
If vz(z, 1) = "" Then Exit For
    For a = 1 To UBound(va)
        For b = 1 To UBound(vb)
            For c = 1 To UBound(vc)
                For d = 1 To UBound(vd)
                    For e = 1 To UBound(ve)
                        For f = 1 To UBound(vf)
                        dSum = vz(z, 1) + va(a, 1) + vb(b, 1) + vc(c, 1) + vd(d, 1) + ve(e, 1) + vf(f, 1)
                        If dSum >= mn And dSum <= mx Then
                        'If dSum = mn Then
                            n = n + 1
                            vx(n, 1) = vz(z, 1)
                            vx(n, 2) = va(a, 1)
                            vx(n, 3) = vb(b, 1)
                            vx(n, 4) = vc(c, 1)
                            vx(n, 5) = vd(d, 1)
                            vx(n, 6) = ve(e, 1)
                            vx(n, 7) = vf(f, 1)
                        
                        If n >= ax Then
                        MsgBox "The result is more than " & ax & " rows. This macro will stop"
                        Exit Sub
                        End If
                        
                        End If
                        Next
                    Next
                Next
            Next
        Next
    Next
Next

Range("N4").Resize(n, 7) = vx

End Sub
Akuini, :pray: I heartily appreciate a lot your help for fulfilling my 3rd request. Thank you so much.

Using your example, all combination will reach 9^7 = 4,782,969 rows & 7 columns.
I did not imagined there could be so much combination. As per example Post#7, if the min sum is = 0 and max sum can be = 162 is there any method to list out how much combinations can be made with each sum 0 to 162? </SPAN>

I tried to run the code with min = 34 and max = 34 got message " The result is more than 65000 rows. This macro will stop" this mean there are more than 65000 with sum 34 how much don't know, used min & max sum 33 it generated 64357 perfectly</SPAN></SPAN>

</SPAN>

A worksheet in Excel 2000 is limited to 65,536 rows & 256 column. If you want to get all the 4,782,969 rows x 7 columns, there's a workaround. We can put the result per 65000 rows, and maybe we can use about 250 column & we may need 3 worksheets.
Let me know if this approach suit you.
Akuini, instead of using 7 columns for each row, would it be possible to create combinations in the single column separated by vertical bar like this 0|0|0|0|0|0|0. If so 4,782,969 / 65000 = 73.58 nearly 74-columns and if one space is used in-betweens of each columns total column will be 148-columns and all can be generated in a single sheet. It is just my thought. If This is possible than I imagine can be used min = 34 & max = 35 to generate sum with 2 sums or using min = 0 & max = 160 to generate all at once in a single sheet</SPAN></SPAN>

Please give your opinion. Programmatically may be it is possible or not?
</SPAN></SPAN>

Kind Regards
</SPAN></SPAN>
Moti
</SPAN></SPAN>
 
Upvote 0
Akuini, instead of using 7 columns for each row, would it be possible to create combinations in the single column separated by vertical bar like this 0|0|0|0|0|0|0. If so 4,782,969 / 65000 = 73.58 nearly 74-columns and if one space is used in-betweens of each columns total column will be 148-columns and all can be generated in a single sheet. It is just my thought. If This is possible than I imagine can be used min = 34 & max = 35 to generate sum with 2 sums or using min = 0 & max = 160 to generate all at once in a single sheet

Please give your opinion. Programmatically may be it is possible or not?


Kind Regards

Moti

Ok, try this:
- it generates all the combinantion in sheet2
- the result does not include the sum of each combination, but if you need it we can modify the code.

Code:
Sub a1080399d()
'https://www.mrexcel.com/forum/excel-questions/1080399-conditional-combinations-pick-only-1-number-each-column-row.html
Dim a As Long, b As Long, c As Long, d As Long, e As Long, f As Long, n As Long
Dim va, vb, vc, vd, ve, vf, vx


Application.ScreenUpdating = False
vz = Range("D4:D12")
va = Range("E4", Cells(Rows.count, "E").End(xlUp))
vb = Range("F4", Cells(Rows.count, "F").End(xlUp))
vc = Range("G4", Cells(Rows.count, "G").End(xlUp))
vd = Range("H4", Cells(Rows.count, "H").End(xlUp))
ve = Range("I4", Cells(Rows.count, "I").End(xlUp))
vf = Range("J4", Cells(Rows.count, "J").End(xlUp))


'mn = Range("B1")
'mx = Range("B2")

ax = 65000
ReDim vx(1 To ax, 1 To 250)
k = 1
For z = 1 To UBound(vz)
If vz(z, 1) = "" Then Exit For
    For a = 1 To UBound(va)
        For b = 1 To UBound(vb)
            For c = 1 To UBound(vc)
                For d = 1 To UBound(vd)
                    For e = 1 To UBound(ve)
                        For f = 1 To UBound(vf)
                        'dSum = vz(z, 1) + va(a, 1) + vb(b, 1) + vc(c, 1) + vd(d, 1) + ve(e, 1) + vf(f, 1)
                        'If dSum >= mn And dSum <= mx Then
                        'If dSum = mn Then
                            n = n + 1
                            vx(n, k) = vz(z, 1) & "|" & va(a, 1) & "|" & vb(b, 1) & "|" & vc(c, 1) & "|" _
                            & vd(d, 1) & "|" & ve(e, 1) & "|" & vf(f, 1)
                       
                        If n Mod 65000 = 0 Then n = 0: k = k + 2
                        
                        'End If
                        Next
                    Next
                Next
            Next
        Next
    Next
Next


Sheets("Sheet2").Range("A2").Resize(ax, k) = vx
Application.ScreenUpdating = True


End Sub
 
Upvote 0

Forum statistics

Threads
1,214,780
Messages
6,121,525
Members
449,037
Latest member
tmmotairi

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