Juggler_IN
Active Member
- Joined
- Nov 19, 2014
- Messages
- 349
- Office Version
- 2003 or older
- Platform
- Windows
I want to output the first n of the total permutations. The code below outputs the complete list of the permutations of a given string. For example, for x=1234 there are 24 possible permutations. How can I limit the output to first, say 10, permutations instead of all 24?
VBA Code:
Sub Text()
Dim x As String, k As Long
Application.ScreenUpdating = False
x = "1234"
ActiveSheet.Columns(1).Clear
k = 1
Call PERM("", x, k)
Application.ScreenUpdating = True
End Sub
Sub PERM(a As String, b As String, ByRef k As Long)
Dim i As Integer, n As Integer, x
n = Len(b)
If n < 2 Then
Range("A" & k) = "'" & a & b
k = k + 1
Else
For i = 1 To n
Call PERM(a + Mid(b, i, 1), Left(b, i - 1) + Right(b, n - i), k)
Next
End If
End Sub