Hi sss,
Thanks for the explanations , they make it more understandable.
I started again and merged your Macro into the Original Macro. After moving Code around a bit I came up with a new Macro ( Listed Below ). I ran it and it worked great . I checked the small output in a seperate SpreadSheet by applying formulas, and the results were ALL OK.
There is one small point though, when I ran your Macro it started putting the Combinations in Cell A1 onwards, now when I run the new Macro it starts putting the Combinations from Cell A2 onwards , I would prefer it to start in Cell A1.
Here is the new Macro :-
Option Explicit
Option Base 1
Public A As Integer
Public B As Integer
Public C As Integer
Public D As Integer
Public E As Integer
Public F As Integer
Dim N As Long
Dim nMinA As Integer
Dim nMaxF As Integer
Dim CriteriaColumns As Collection
Dim ColumnNumbers() As Integer
Dim TotalColumns As Integer
Dim ColumnCount() As Integer
Sub Combinations_649_A()
Dim ThisCriteria As Integer
Range("A1").Select
Application.ScreenUpdating = False
getCriteria
N = 1
Selection.ColumnWidth = 18
nMinA = 1
nMaxF = 16
For A = nMinA To nMaxF - 5
For B = A + 1 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
End If
If SumAF Then
ThisCriteria = TestCriteria
If ThisCriteria = 321 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")
End If
End If
Next F
Next E
Next D
Next C
Next B
Next A
Application.ScreenUpdating = True
Set CriteriaColumns = Nothing
End Sub
Function SumAF()
SumAF = False
If A + B + C + D + E + F >= 21 And A + B + C + D + E + F <= 55 Then SumAF = True
End Function
Private Function TestCriteria() As Integer
Dim Done As Boolean
Dim i&, j&, Column&
Dim Arr(1 To 6) As Integer
Dim Ball As Integer
Dim Maximum As Integer
Dim strResult As String
Dim z As Variant
Arr(1) = A: Arr(2) = B: Arr(3) = C: Arr(4) = D: Arr(5) = E: Arr(6) = F
For Ball = 1 To 6
Done = False
For Column = 1 To TotalColumns
z = CriteriaColumns(Column)
j = UBound(z)
For i = 1 To j
If Arr(Ball) = z(i) Then
Done = True
Exit For
End If
Next
If Done Then Exit For
Next
ColumnCount(Column) = ColumnCount(Column) + 1
Next
strResult = ""
For i = 1 To TotalColumns
Maximum = -1
For j = 1 To TotalColumns
If ColumnCount(j) >= Maximum Then
Maximum = ColumnCount(j)
Ball = j
End If
Next
If Maximum > 0 Then strResult = strResult & Maximum
ColumnCount(Ball) = 0
Next
TestCriteria = CInt(strResult)
End Function
Private Sub getCriteria()
Dim col&, row&
Sheets("Group Criteria").Select
Set CriteriaColumns = New Collection
col = 1
Do While Trim(Cells(1, col)) <> ""
row = 1
ReDim ColumnNumbers(1)
Do While Trim(Cells(row, col)) <> ""
ReDim Preserve ColumnNumbers(row)
ColumnNumbers(row) = Cells(row, col)
row = row + 1
Loop
CriteriaColumns.Add ColumnNumbers
col = col + 1
Loop
TotalColumns = CriteriaColumns.Count
ReDim ColumnCount(TotalColumns)
Sheets("Combinations").Select
End Sub
I will play around trying out adding new Functions over the next couple of days.
Thanks again for all your efforts .
May the Force be with you
All the Best
PAB
Thanks for the explanations , they make it more understandable.
I started again and merged your Macro into the Original Macro. After moving Code around a bit I came up with a new Macro ( Listed Below ). I ran it and it worked great . I checked the small output in a seperate SpreadSheet by applying formulas, and the results were ALL OK.
There is one small point though, when I ran your Macro it started putting the Combinations in Cell A1 onwards, now when I run the new Macro it starts putting the Combinations from Cell A2 onwards , I would prefer it to start in Cell A1.
Here is the new Macro :-
Option Explicit
Option Base 1
Public A As Integer
Public B As Integer
Public C As Integer
Public D As Integer
Public E As Integer
Public F As Integer
Dim N As Long
Dim nMinA As Integer
Dim nMaxF As Integer
Dim CriteriaColumns As Collection
Dim ColumnNumbers() As Integer
Dim TotalColumns As Integer
Dim ColumnCount() As Integer
Sub Combinations_649_A()
Dim ThisCriteria As Integer
Range("A1").Select
Application.ScreenUpdating = False
getCriteria
N = 1
Selection.ColumnWidth = 18
nMinA = 1
nMaxF = 16
For A = nMinA To nMaxF - 5
For B = A + 1 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
End If
If SumAF Then
ThisCriteria = TestCriteria
If ThisCriteria = 321 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")
End If
End If
Next F
Next E
Next D
Next C
Next B
Next A
Application.ScreenUpdating = True
Set CriteriaColumns = Nothing
End Sub
Function SumAF()
SumAF = False
If A + B + C + D + E + F >= 21 And A + B + C + D + E + F <= 55 Then SumAF = True
End Function
Private Function TestCriteria() As Integer
Dim Done As Boolean
Dim i&, j&, Column&
Dim Arr(1 To 6) As Integer
Dim Ball As Integer
Dim Maximum As Integer
Dim strResult As String
Dim z As Variant
Arr(1) = A: Arr(2) = B: Arr(3) = C: Arr(4) = D: Arr(5) = E: Arr(6) = F
For Ball = 1 To 6
Done = False
For Column = 1 To TotalColumns
z = CriteriaColumns(Column)
j = UBound(z)
For i = 1 To j
If Arr(Ball) = z(i) Then
Done = True
Exit For
End If
Next
If Done Then Exit For
Next
ColumnCount(Column) = ColumnCount(Column) + 1
Next
strResult = ""
For i = 1 To TotalColumns
Maximum = -1
For j = 1 To TotalColumns
If ColumnCount(j) >= Maximum Then
Maximum = ColumnCount(j)
Ball = j
End If
Next
If Maximum > 0 Then strResult = strResult & Maximum
ColumnCount(Ball) = 0
Next
TestCriteria = CInt(strResult)
End Function
Private Sub getCriteria()
Dim col&, row&
Sheets("Group Criteria").Select
Set CriteriaColumns = New Collection
col = 1
Do While Trim(Cells(1, col)) <> ""
row = 1
ReDim ColumnNumbers(1)
Do While Trim(Cells(row, col)) <> ""
ReDim Preserve ColumnNumbers(row)
ColumnNumbers(row) = Cells(row, col)
row = row + 1
Loop
CriteriaColumns.Add ColumnNumbers
col = col + 1
Loop
TotalColumns = CriteriaColumns.Count
ReDim ColumnCount(TotalColumns)
Sheets("Combinations").Select
End Sub
I will play around trying out adding new Functions over the next couple of days.
Thanks again for all your efforts .
May the Force be with you
All the Best
PAB