How to generate or list all possible permutations in Excel?
For example, I have three characters XYZ, now, I want to list all possible permutations based on these three characters to get six different results as this: XYZ, XZY, YXZ, YZX, ZXY and ZYX. In Excel, how could you quickly generate or list all permutations based on different number of characters?
List All Possible Combinations from multiple columns:
Kutools for Excel’s List All Combinations utility can help you quickly generate / list all possible combinations based on multiple lists of values.
Kutools for Excel: with more than 200 handy Excel add-ins, free to try with no limitation in 60 days. Download and free trial Now!
The following VBA code may help you to list all permutations based on your specific number of letters please do as follows:
1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.
2. Click Insert > Module, and paste the following code in the Module Window.
VBA code: List all possible permutations in excel
Sub GetString() 'Updateby Extendoffice 20160606 Dim xStr As String Dim FRow As Long Dim xScreen As Boolean xScreen = Application.ScreenUpdating Application.ScreenUpdating = False xStr = Application.InputBox("Enter text to permute:", "Kutools for Excel", , , , , , 2) If Len(xStr) < 2 Then Exit Sub If Len(xStr) >= 8 Then MsgBox "Too many permutations!", vbInformation, "Kutools for Excel" Exit Sub Else ActiveSheet.Columns(1).Clear FRow = 1 Call GetPermutation("", xStr, FRow) End If Application.ScreenUpdating = xScreen End Sub Sub GetPermutation(Str1 As String, Str2 As String, ByRef xRow As Long) Dim i As Integer, xLen As Integer xLen = Len(Str2) If xLen < 2 Then Range("A" & xRow) = Str1 & Str2 xRow = xRow + 1 Else For i = 1 To xLen Call GetPermutation(Str1 + Mid(Str2, i, 1), Left(Str2, i - 1) + Right(Str2, xLen - i), xRow) Next End If End Sub
3. Then press F5 key to run this code, and a prompt box is popped out to remind you enter the characters that you want to list all permutations, see screenshot:
4. After entering the characters, and then click OK button, all the possible permutations are displayed in column A of active worksheet. See screenshot:
Note: If the entered character length is equal or greater than 8 characters, this code will not work because there are too many permutations.
Recommended Productivity Tools
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
To post as a guest, your comment is unpublished.· 4 days agopeki bunu listeleyecek bir program uygulama yok mu?basit sıradan bir hesaplamadan daha fazlasına ihtiyacı olan ne yapacak?
To post as a guest, your comment is unpublished.· 28 days agothis code will not work because there are two many permutations
this code will not work because there are too many permutations
To post as a guest, your comment is unpublished.· 11 months agoHello everyone. I need help on this. I have two alphabets to be permutated in 20 rows. But am not getting it right. Anyone who could help me out should send the permutation to my email. email@example.com.
1.a b b a
2.a a b b
3.a a b b
4.a a b b
5.a a b b
6.a a b b
7.a a b b
8.a a b b
9.a a b b
10.a a b b
11.a a b b
12.a a b b
13.a a b b
14.a a b b
15.a a b b
16.a a b b
17.a a b b
18.a a b b
19.a a b b
20.a a b b
To post as a guest, your comment is unpublished.
To post as a guest, your comment is unpublished.· 1 years ago@Supraja...
in the first sub clear all cells... not just the first row
Sub GetPermutation(Str1 As String, Str2 As String, ByRef xRow As Long)
Dim i As Integer, xLen As Integer
xLen = Len(Str2)
If xLen < 2 Then
'move to the next column when you get to 100
Cells(((xRow - 1) Mod 100) + 1, 1 + Int(xRow / 100)) = Str1 & Str2
xRow = xRow + 1
For i = 1 To xLen
Call GetPermutation(Str1 + Mid(Str2, i, 1), Left(Str2, i - 1) + Right(Str2, xLen - i), xRow)