|
Sub PermuteAndPrint() Dim str As String str = "ABCDEFGHIJKLMNOPQ" Dim arr() As String ReDim arr(1 To Len(str)) For i = 1 To Len(str) arr(i) = Mid(str, i, 1) Next i Dim output() As String ReDim output(1 To WorksheetFunction.Fact(Len(str))) Permute arr, 1, Len(str), output Range("A1").Resize(UBound(output)) = Application.Transpose(output)End SubSub Permute(ByVal arr() As String, ByVal l As Long, ByVal r As Long, ByRef output() As String) If l = r Then output(UBound(output)) = Join(arr, "") ReDim Preserve output(1 To UBound(output) + 1) Else For i = l To r Swap arr(l), arr(i) Permute arr, l + 1, r, output Swap arr(l), arr(i) Next i End IfEnd SubSub Swap(ByRef a As String, ByRef b As String) Dim temp As String temp = a a = b b = tempEnd Sub |
|