You can do this quickly with a little bit of VBA.
First, let's calculate the actual number of permutations: =PERMUT(5,5) = 120.
To list them, we use the UDF below.
We select E2:I121 since we know there are 121 permutations to cover. Next we enter =ListPermut(5) and use Ctrl+Shift+Enter to exit the cell and apply the array formula to all cells simultaneously.
Function ListPermut(num As Integer)
Dim c As Long, r As Long, p As Long
Dim rng() As Long, temp As Long, i As Long
Dim temp1 As Long, y() As Long, d As Long
p = WorksheetFunction.Permut(num, num)
ReDim rng(1 To p, 1 To num)
For c = 1 To num
rng(1, c) = c
Next c
For r = 2 To p
For c = num To 1 Step -1
If rng(r - 1, c - 1) < rng(r - 1, c) Then
temp = c - 1
Exit For
End If
Next c
For c = num To 1 Step -1
rng(r, c) = rng(r - 1, c)
Next c
For c = num To 1 Step -1
If rng(r - 1, c) > rng(r - 1, temp) Then
temp1 = rng(r - 1, temp)
rng(r, temp) = rng(r - 1, c)
rng(r, c) = temp1
ReDim y(num - temp)
i = 0
For d = temp + 1 To num
y(i) = rng(r, d)
i = i + 1
Next d
i = 0
For d = num To temp + 1 Step -1
rng(r, d) = y(i)
i = i + 1
Next d
Exit For
End If
Next c
Next r
ListPermut = rng
End Function
How to install your new code
- Copy the Excel VBA code
- Select the workbook in which you want to store the Excel VBA code
- Press Alt+F11 to open the Visual Basic Editor
- Choose Insert > Module
- Edit > Paste the macro into the module that appeared
- Close the VBEditor
- Save your workbook (Excel 2007+ select a macro-enabled file format, like *.xlsm)
Bookmarks