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?

Generate or list all possible permutations based on characters with VBA code

Generate or list all possible permutations based on characters with VBA code

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
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.

List or generate all possible combinations from multiple columns

If you need to generate all possible combinations based on multiple columns data, maybe, there is not a good way for dealing with the task. But, Kutools for Excel's List All Combinations utility can help you to list all possible combinations quickly and easily. Click to download Kutools for Excel!

Kutools for Excel: with more than 300 handy Excel add-ins, free to try with no limitation in 30 days. Download and free trial Now!

Best Office Productivity Tools

 🤖 Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functions… Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |  Round without Formula ... Super Lookup: Multiple Criteria VLookup  |   Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup .... Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List .... Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ... Featured Features: Grid Focus   |  Design View   |  Big Formula Bar   |  Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells   |  Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ... Top 15 Toolsets:  12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more

Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time.  Click Here to Get The Feature You Need The Most...

Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

• Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
• Open and create multiple documents in new tabs of the same window, rather than in new windows.
• Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
This comment was minimized by the moderator on the site
=LET(a,A2,i,LEN(a),IFERROR(TOROW(BYROW(MID(a,MID(BASE(SEQUENCE(i^i)-1,i,i),SEQUENCE(,i),1)+1,1),LAMBDA(A,
IFS(COUNTA(UNIQUE(A,1))=i,CONCAT(A)))),2),a))

i^i=PERMUTATIONA(i,i)
This comment was minimized by the moderator on the site
Olá !

Como faço para gerar pelo menos 10 permutações ?
This comment was minimized by the moderator on the site
Hello, Mateus,
To solve your problem, please apply the below code:(Note: if there are more than 8 characters, the code will execute slowly.)
``````Sub GetString()
'Updateby Extendoffice
Dim xStr As String
Dim FRow As Long
Dim FC As Integer
Dim xScreen As Boolean
Dim xNumber As Long
xNumber=10 ' This is the max length of the characters you can change it to 11, 12, 13...as you need
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) > xNumber Then
MsgBox "Too many permutations!", vbInformation, "Kutools for Excel"
Exit Sub
Else
ActiveSheet.Columns(1).Clear
FRow=1
FC=1
Call GetPermutation("", xStr, FRow, FC)
End If
Application.ScreenUpdating=xScreen
End Sub
Sub GetPermutation(Str1 As String, Str2 As String, ByRef xRow As Long, ByRef xc As Integer)
Dim i As Integer, xLen As Integer
xLen=Len(Str2)
If xLen < 2 Then
If xRow > 1000000 Then
xc=xc + 1
xRow=1
End If
ActiveSheet.Cells(xRow, xc)=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, xc)
Next
End If
End Sub``````

This comment was minimized by the moderator on the site
Hi there, if the input string contains duplicate chars, then the sub produces duplicate permutations.
This does not happen if you make the following modification the the loop:

' ==========================
For i=1 To xLen
If Instr( Left(Str2, i - 1), Mid(Str2, i, 1) )=0 then
Call GetPermutation(Str1 + Mid(Str2, i, 1), Left(Str2, i - 1) + Right(Str2, xLen - i), xRow)
End if
Next
' ==========================

Creating temporary local variables for Mid(Str2, i, 1) and for Left(Str2, i - 1), and avoiding the test for i=1 makes it go faster:

' ==========================
Sub GetPermutation(Str1 As String, Str2 As String, ByRef xRow As Long)
Dim i As Integer, xLen As Integer, Str2left as String, c as String
xLen=Len(Str2)
If xLen < 2 Then
Range("A" & xRow)=Str1 & Str2
xRow=xRow + 1
Else
Call GetPermutation(Str1 + Mid(Str2, 1, 1), Right(Str2, xLen - 1), xRow)
For i=2 To xLen
c=Mid(Str2, i, 1)
Str2left=Left(Str2, i - 1)
If Instr( Str2left, c )=0 then
Call GetPermutation(Str1 + c, Str2left + Right(Str2, xLen - i), xRow)
End If
Next
End If
End Sub
' ==========================

Cheers,
DVdm
There are no comments posted here yet