' Main entry point - called from the command button
' (Code for command button is in Sheet2)
Public Sub CalculateWheel()
Dim strRange As String
Dim varData As Variant ' 2-dimensional array holding all selected numbers
Dim lngComb() As Long ' 2-dimensional array holding combinations
Dim x As Long
Dim y As Long
Dim lngMax As Long ' Highest selected value
Dim sngStart As Single
sngStart = Timer
' Identify where output begins. This will fail if you specify a
' column past column Z. Note that the Asc() function returns a
' numeric character code. Asc("A")=65, Asc("B")=66, etc...
' Each subsequent call simply moves down one row.
mlngOutputRow = Mid(OutputStart, 2)
mlngOutputColumn = Asc(Left(OutputStart, 1)) - 64
If UseLogFile Then
' Get full path and filename of active workbook
mstrLogFile = ActiveWorkbook.FullName
' Use same path & name, but change extension to ".log"
mstrLogFile = Left(mstrLogFile, InStrRev(mstrLogFile, ".")) & "log"
' Get available file handle
FileNumber = FreeFile()
' Create log file (overwrite existing)
Open mstrLogFile For Output As #FileNumber
End If
' Identify data block (see constants defined at top of module)
Sheets(StatsSheet).Select
' Check spreadsheet values; if found, use them...
If Range(NumbersSelected).Value <> 0 And Range(NumbersDrawn).Value <> 0 Then
x = Asc(Left(DataTopLeft, 1)) + Range(NumbersDrawn).Value - 65
y = Val(Mid(DataTopLeft, 2)) + Range(NumbersSelected).Value - 1
strRange = DataTopLeft & ":" & GetColumnLetter(x) & y
Else ' ...otherwise just use all data found
Range(DataTopLeft).Select
ActiveCell.SpecialCells(xlLastCell).Select
strRange = DataTopLeft & ":" & GetColumnLetter(ActiveCell.Column) & ActiveCell.Row
End If
Sheets(DataSheet).Select
Range(strRange).Select
' Load selected numbers into array
varData = Range(strRange)
mlngRows = UBound(varData, 1)
mlngCols = UBound(varData, 2)
' Identify largest value by comparing against every value in matrix
For x = 1 To mlngRows
For y = 1 To mlngCols
If varData(x, y) > lngMax Then lngMax = varData(x, y)
Next
Next
If UseLogFile Then
Print #FileNumber, "Raw data (Highest number: " & lngMax & ")" & vbNewLine & "--------"
DisplayArray varData, True
End If
' Generate all the combinations of 2 through mlngCols numbers
For y = 2 To mlngCols
' Store all 2-, 3-, 4- or 5-number combinations to lngComb()
GetCombinations lngComb, lngMax, y
If UseLogFile Then
If y <> 2 Then Print #FileNumber, ""
Print #FileNumber, UBound(lngComb, 2) + 1 & " combinations of " & y & " numbers" & vbNewLine & String(Len(UBound(lngComb, 2) + 1 & " combinations of " & y & " numbers"), 45)
End If
' Locate matches
IdentifyMatches varData, lngComb, y
' Free memory
Erase lngComb
Next
If UseLogFile Then Close #FileNumber
' Free memory
Erase varData
Sheets(StatsSheet).Select
MsgBox "Processing completed successfully in " & SecondsToTime(Timer - sngStart) & " seconds", vbInformation, "Notice"
End Sub