Via code:
Sub x()
Const sSym As String = "0124569"
Dim aiC(1 To 2) As Long
Dim s As String
Dim asOut() As String
aiC(1) = -1
Do While bNextCombo(aiC, 7)
s = "378" & Mid(sSym, aiC(1) + 1, 1) & Mid(sSym, aiC(2) + 1, 1)
bStrPermute s, asOut
Cells(Rows.Count, "A").End(xlUp)(2).Resize(UBound(asOut)).Value = asOut
Loop
End Sub
Public Function bNextCombo(aiC() As Long, n As Long) As Boolean
' shg 2009-12
' 2011-07 (modified to require aiC(1) < 0 to initialize)
' VBA only
' Sets aiC to the next combination of n choose m in lexical order
' Returns True unless the combination is the last, in which case
' it leaves aiC unmodified.
' If aiC(1) < 0, initializes aiC to the first combo:
' {m-1, m-2, ..., 1, 0}
' The last combo is {n-1, n-2, ..., n-m+1, n-m}
Dim m As Long
Dim i As Long
m = UBound(aiC)
If n < m Then Exit Function
If aiC(1) < 0 Then ' set initial combo
i = 1
aiC(1) = m - 2
Else
' find rightmost incrementable index
For i = m To 2 Step -1
If aiC(i) < aiC(i - 1) - 1 Then Exit For
Next i
End If
If i <> 1 Or aiC(1) < n - 1 Then
' increment that index, and set 'righter' indices descending to 0
aiC(i) = aiC(i) + 1
For i = i + 1 To m
aiC(i) = m - i
Next i
bNextCombo = True
End If
End Function
Function bStrPermute(sInp As String, asOut() As String) As Boolean
' puts the permutations of sInp in dynamic array asOut
' VBA only
If Len(sInp) < 9 Then
ReDim asOut(1 To WorksheetFunction.Fact(Len(sInp)), 1 To 1)
GetPermutation "", sInp, asOut, 0
bStrPermute = True
End If
End Function
Sub GetPermutation(sL As String, sR As String, asOut() As String, nOut As Long)
' adapted from http://spreadsheetpage.com/index.php/site/tip/generating_permutations/
Dim i As Integer
Dim j As Integer
j = Len(sR)
If j <= 1 Then
nOut = nOut + 1
asOut(nOut, 1) = sL & sR
Else
For i = 1 To j
GetPermutation sL & Mid(sR, i, 1), Left(sR, i - 1) & Right(sR, j - i), asOut, nOut
Next
End If
End Sub
Bookmarks