Option Explicit
Option Compare Text
Dim CurrentRow
Sub DoString()
On Error Resume Next
Dim Instring As String
Dim i As Integer, j As Integer
Instring = Range("A1").Value
Range("A1").Select
CurrentRow = 1
Call GetPermutation("", Instring)
End Sub
Sub GetPermutation(X As String, y As String)
On Error Resume Next
Dim j, i
j = Len(y)
If j < 2 Then
Cells(CurrentRow, 1) = 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
Try this, I believe the author of the code "GetPermutation(X As String, y As String)" is unknown, per MVP's of the old MS Exel Users Group.
Does the abcdefg string's 5040 Permutations in a split second.
Code:Option Explicit Option Compare Text Dim CurrentRow Sub DoString() On Error Resume Next Dim Instring As String Dim i As Integer, j As Integer Instring = Range("A1").Value Range("A1").Select CurrentRow = 1 Call GetPermutation("", Instring) End Sub Sub GetPermutation(X As String, y As String) On Error Resume Next Dim j, i j = Len(y) If j < 2 Then Cells(CurrentRow, 1) = 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
Regards,
Howard
Excel 2007 | |||
---|---|---|---|
A | |||
1 | abcdefg | ||
2 | |||
3 | |||
4 | |||
5 | |||
6 | |||
7 | |||
8 | |||
9 | |||
10 | |||
11 | |||
Sheet1 |
Excel 2007 | |||
---|---|---|---|
A | |||
1 | abcdefg | ||
2 | abcdegf | ||
3 | abcdfeg | ||
4 | abcdfge | ||
5 | abcdgef | ||
5035 | gfedabc | ||
5036 | gfedacb | ||
5037 | gfedbac | ||
5038 | gfedbca | ||
5039 | gfedcab | ||
5040 | gfedcba | ||
5041 | |||
Sheet1 |
Option Explicit
Option Compare Text
Dim CurrentRow
Sub DoString()
'' I believe the author of the code "GetPermutation(X As String, y As String)"
'' is unknown, per MVP's of the old MS Exel Users Group.
'' Does the abcdefg string's 5040 Permutations in a split second.
On Error Resume Next
Dim Instring As String
Dim i As Integer, j As Integer
Instring = Range("A1").Value
Range("A1").Select
CurrentRow = 1
Call GetPermutation("", Instring)
End Sub
Sub GetPermutation(X As String, y As String)
On Error Resume Next
Dim j, i
j = Len(y)
If j < 2 Then
Cells(CurrentRow, 1) = 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