Juggler_IN
Active Member
- Joined
- Nov 19, 2014
- Messages
- 349
- Office Version
- 2003 or older
- Platform
- Windows
How can the code for permutations (appended) be modified to output only complete permutations. A complete permutation is a permutation of the order of distinct items in which no item appears in its original place.
VBA Code:
Sub GetTEXT()
Dim x As String, n As Long
Application.ScreenUpdating = False
x = Application.InputBox("Text:", , , , , , , 2)
If Len(x) < 2 Then Exit Sub
If Len(x) >= 8 Then
MsgBox "Too many permutations!"
Exit Sub
Else
ActiveSheet.Columns(1).Clear
n = 1
Call GetPERM("", x, n)
End If
Application.ScreenUpdating = True
End Sub
Sub GetPERM(ByRef u As String, ByRef v As String, ByRef w As Long)
Dim i As Integer, n As Integer
n = Len(v)
If n < 2 Then
Range("A" & w) = u & v
w = w + 1
Else
For i = 1 To n
Call GetPERM(u + Mid(v, i, 1), Left(v, i - 1) + Right(v, n - i), w)
Next
End If
End Sub