Listing of combinations
PAB,
I have modified the macro to be as flexible as possible. Examples are given for the criteria you mentioned (sum meeting certain values, not all even, odd or prime numbers, no three consecutive numbers, specific range, minimum and maximum values). But you can remove any criteria you do not want or even better, develop your own.
The macro List_Comb will list all combinations in the form N1-N2-N3-N4-N5-N6 in adjacent columns starting at A. You can modify the minimum value for the first number and the maximum value for the last number by changing respectively the values for MinA and MaxF (set in the macro for 1 and 49) near the top of the macro.
Before listing a combination in cell, the macro use a function to verify if the values A to F meet a criteria. Any number of criteria can be set and all of them must be met if the combination is to be listed. In this case, the functions are respectively SumAF (here set between 135 and 155), RangeAF (here set at lower or equal to 40), Not3Cons, NotAllEven, NotAllOdd and NotAllPrime.
When removing or adding any call to a function by the statement “If Function then” in the section identified for this, make sure there are as many “End If” statements in the section following the statement “Activecell.Value ….”. The order for the criteria may affect the speed of the macro and it will probably run faster if criteria that eliminate the most combinations are placed at the top.
I hope the explanations are clear enough but ask if not satisfactory. I have not fully tested the macro with all criteria; let me know if there is a bug.
Here are the macro and the various functions:
Option Explicit
Public A As Integer, B As Integer, C As Integer, D As Integer, E As Integer, F As Integer
Sub List_Comb()
Dim N As Long, nMinA As Integer, nMaxF As Integer
Range("A1").Select
Application.ScreenUpdating = False
N = 1
Selection.ColumnWidth = 18
ActiveCell.Value = "Comb."
' Change minimum value for A or maximum value for F if appropriate
nMinA = 1
nMaxF = 49
For A = nMinA To nMaxF - 5
For B = A + 2 To nMaxF - 4
For C = B + 1 To nMaxF - 3
For D = C + 1 To nMaxF - 2
For E = D + 1 To nMaxF - 1
For F = E + 1 To nMaxF
If N = 65001 Then
Selection.ColumnWidth = 18
N = 1
ActiveCell.Offset(-65000, 1).Select
Application.ScreenUpdating = True
Application.ScreenUpdating = False
ActiveCell.Value = "Comb."
End If
' Insert here as many IF ... statements as necessary to list combinations meeting your criteria
If SumAF Then
If RangeAF Then
If Not3Cons Then
If NotAllPrime Then
If NotAllEven Then
If NotAllOdd Then
ActiveCell.Offset(1, 0).Select
N = N + 1
ActiveCell.Value = Application.WorksheetFunction.Text(A, "00") & "-" _
& Application.WorksheetFunction.Text(B, "00") & "-" _
& Application.WorksheetFunction.Text(C, "00") & "-" _
& Application.WorksheetFunction.Text(D, "00") & "-" _
& Application.WorksheetFunction.Text(E, "00") & "-" _
& Application.WorksheetFunction.Text(F, "00")
' Insert here as many END IF statements as you added IF ... Then statements
End If
End If
End If
End If
End If
End If
Next F
Next E
Next D
Next C
Next B
Next A
Application.ScreenUpdating = True
End Sub
Function SumAF()
' Return True if the condition for the sum (A - F) is satisfied
SumAF = False
If A + B + C + D + E + F >= 135 And A + B + C + D + E + F <= 155 Then SumAF = True
End Function
Function RangeAF()
' Return TRUE if the condition for the range A - F is satisfied
RangeAF = False
If F - A <= 40 Then RangeAF = True
End Function
Function Not3Cons()
' Return True if there is no 3 consecutive numbers (A to F)
Not3Cons = False
If C - A > 2 And D - B > 2 And E - C > 2 And F - D > 2 Then Not3Cons = True
End Function
Function NotAllEven()
' Return True if at least one number (A to F) is odd
NotAllEven = False
Select Case A
Case 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49
NotAllEven = True
Exit Function
End Select
Select Case B
Case 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49
NotAllEven = True
Exit Function
End Select
Select Case C
Case 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49
NotAllEven = True
Exit Function
End Select
Select Case D
Case 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49
NotAllEven = True
Exit Function
End Select
Select Case E
Case 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49
NotAllEven = True
Exit Function
End Select
Select Case F
Case 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49
NotAllEven = True
Exit Function
End Select
End Function
Function NotAllOdd()
' Return True if at least one number (A to F) is even
NotAllOdd = False
Select Case A
Case 2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48
NotAllEven = True
Exit Function
End Select
Select Case B
Case 2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48
NotAllOdd = True
Exit Function
End Select
Select Case C
Case 2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48
NotAllOdd = True
Exit Function
End Select
Select Case D
Case 2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48
NotAllOdd = True
Exit Function
End Select
Select Case E
Case 2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48
NotAllOdd = True
Exit Function
End Select
Select Case F
Case 2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48
NotAllOdd = True
Exit Function
End Select
End Function
Function NotAllPrime()
' Return True if at least one number (A to F) is not prime
NotAllPrime = True
Select Case A
Case 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47
Select Case B
Case 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47
Select Case C
Case 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47
Select Case D
Case 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47
Select Case E
Case 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47
Select Case F
Case 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47
NotAllPrime = False
Exit Function
End Select
End Select
End Select
End Select
End Select
End Select
End Function