Public Sub doIt()
Dim thesheet As Worksheet
Dim thebook As Workbook
On Error GoTo waserr
Set thebook = ActiveWorkbook
Set thesheet = thebook.ActiveSheet
Dim strDigits As String, strNumDigits As String, iNumDigits As Long
Dim theDigits
strDigits = thesheet.Cells(2, 1)
strNumDigits = thesheet.Cells(2, 2)
Err.Clear
iNumDigits = CLng(strNumDigits)
If Err.Number <> 0 Then
GoTo waserr
End If
If strDigits <> "" Then
Dim d As Long, thePositions, thePositionDigitIndexes
Dim curNum As String, curIndex As Long
theDigits = Split(strDigits, " ")
ReDim Preserve theDigits(UBound(theDigits))
ReDim thePositions(iNumDigits - 1)
ReDim thePositionDigitIndexes(iNumDigits - 1)
For d = 0 To UBound(thePositions)
thePositions(d) = theDigits(0)
thePositionDigitIndexes(d) = 0
Next
curIndex = 1
curNum = ""
Dim numTimesToDo As Long
numTimesToDo = UBound(theDigits) + 1
For d = 1 To UBound(thePositions)
numTimesToDo = numTimesToDo * (UBound(theDigits) + 1)
Next
Dim curStuff As Long, rowOffset As Long, columnOffset As Long
rowOffset = 0
columnOffset = 0
For curStuff = 1 To numTimesToDo
For d = UBound(thePositions) To 0 Step -1
thePositions(d) = theDigits(thePositionDigitIndexes(d))
Next
curNum = Join(thePositions, "")
If curIndex > 65530 Then
columnOffset = columnOffset + 2
curIndex = 1
End If
thesheet.Cells(curIndex + 2, columnOffset + 1) = curStuff
thesheet.Cells(curIndex + 2, columnOffset + 2) = curNum
If curStuff = numTimesToDo Then
Exit For
End If
'1 2 3 / 3
'0 0 0 => 1 1 1
'0 0 1 => 1 1 2
'0 0 2 => 1 1 3
'0 0 3 => asdf 0 1 0
Dim lastIndex As Long
'if last position index = last digit, set it to first digit, & set next position to next digit
lastIndex = thePositionDigitIndexes(UBound(thePositionDigitIndexes))
lastIndex = lastIndex + 1
thePositionDigitIndexes(UBound(thePositionDigitIndexes)) = lastIndex
For d = UBound(thePositionDigitIndexes) To 0 Step -1
Dim thisIndex As Long
thisIndex = thePositionDigitIndexes(d)
If thisIndex > UBound(theDigits) Then
thisIndex = 0
thePositionDigitIndexes(d - 1) = thePositionDigitIndexes(d - 1) + 1
thePositionDigitIndexes(d) = thisIndex
End If
Next
curIndex = curIndex + 1
Next
End If
GoTo getout
waserr:
MsgBox Err.Description, vbCritical, "error"
getout:
MsgBox "i'm out this *****"
Exit Sub
End Sub