Dim CurrentRow As Long
Dim Combis(1 To 360360) As String
'The above lines in the Declaration area at the top of the module.
Sub Combins()
'NOTE, IMPORTANT! At the top of the module there should be the following 2 lines (not commneted out, of course) to make global variables:
'Dim CurrentRow As Long
'Dim Combis(1 To 360360) As String
Application.ScreenUpdating = False
Dim combistr As String
Set wf = Application.WorksheetFunction
x = 0
CurrentRow = 1
For i = 1 To 11
For j = i + 1 To 12
For k = j + 1 To 13
For m = k + 1 To 14
For n = m + 1 To 15
'because there are only 15 numbers, I converted to Hexadecimal so that all resulting strings would be 5 characters long.
combistr = wf.Dec2Hex(i) & wf.Dec2Hex(j) & wf.Dec2Hex(k) & wf.Dec2Hex(m) & wf.Dec2Hex(n)
'Debug.Print combistr
GetPermutation "", combistr
x = x + 1
Next n
Next m
Next k
Next j
Next i
'Debug.Print x
'The following writes the array to column A in blocks of 65536 as it doesn't like writing the array in one go:
'Later I added Hex to Decimal conversion.
Dim yyy(1 To 65536)
x = UBound(Combis)
Range("A1").Resize(x).NumberFormat = "@"
For i = 1 To x Step 65536
Erase yyy
ThisArrayMax = Application.Min(65536, x - i + 1)
For j = 1 To ThisArrayMax
'yyy(j) = Combis(i + j - 1)
OriginalString = Combis(i + j - 1)
NewString = ""
For k = 1 To 5
NewString = NewString & wf.Hex2Dec(Mid(OriginalString, k, 1)) & ","
Next k
yyy(j) = Left(NewString, Len(NewString) - 1)
Next j
ActiveSheet.Range("A" & i).Resize(ThisArrayMax).Value = wf.Transpose(yyy)
'ActiveWindow.ScrollRow = i + j - 5
Next i
'Now sort randomly (you can comment out this block to retain an ordered list):
With Range("B1").Resize(x)
.Formula = "=rand()"
.Value = .Value
With ActiveSheet.Sort
.SortFields.Clear
.SortFields.Add Key:=Range("B1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange Range("A1").Resize(x, 2)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
.ClearContents
End With
Application.ScreenUpdating = True
End Sub
Sub GetPermutation(x As String, y As String)
'from:http://j-walk.com/ss/excel/tips/tip46.htm
' The source of this algorithm is unknown
Dim i As Integer, j As Integer
j = Len(y)
If j < 2 Then
'Cells(CurrentRow, 1) = x & y
Combis(CurrentRow) = x & y
CurrentRow = CurrentRow + 1
Else
For i = 1 To j
Call GetPermutation(x + Mid(y, i, 1), Left(y, i - 1) + Right(y, j - i))
Next
End If
End Sub