Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

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


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.

doc find combination equal sum 2

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!


arrow blue right bubble Generate or list all possible permutations based on characters with VBA code


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:

doc list permutations 1

4. After entering the characters, and then click OK button, all the possible permutations are displayed in column A of active worksheet. See screenshot:

doc list permutations 2

Note: If the entered character length is equal or greater than 8 characters, this code will not work because there are too many permutations.

doc list permutations 3


Recommended Productivity Tools

Ribbon of Excel (with Kutools for Excel installed)

300+ Advanced Features Increase Your Productivity by 71%, and Help You To Stand Out From Crowd!

Would you like to complete your daily work quickly and perfectly? Kutools For Excel brings 300+ cool and powerful advanced features (Combine workbooks, sum by color, split cell contents, convert date, and so on...) for 1500+ work scenarios, helps you solve 82% Excel problems.

  •  Deal with all complicated tasks in seconds, help to enhance your work ability, get success from the fierce competition, and never worry about being fired.
  •  Save a lot of work time, leave much time for you to love and care the family and enjoy a comfortable life now.
  •  Reduce thousands of keyboard and mouse clicks every day, relieve your tired eyes and hands, and give you a healthy body.
  •  Become an Excel expert in 3 minutes, and get admiring glance from your colleagues or friends.
  •  No longer need to remember any painful formulas and VBA codes, have a relaxing and pleasant mind, give you a thrill you've never had before.
  •  Spend only $39, but worth than $4000 training of others. Being used by 110,000 elites and 300+ well-known companies.
  •  60-day unlimited free trial. 60-day money back guarantee. Free upgrade and support for 2 years. Buy once, use forever.
  •  Change the way you work now, and give you a better life immediately!

Office Tab Brings Efficient And Handy Tabs to Office (include Excel), Just Like Chrome, Firefox, And New IE

  • Increases your productivity by 50% when viewing and editing multiple documents.
  • Reduce hundreds of mouse clicks for you every day, say goodbye to mouse hand.
  • Open and create documents in new tabs of same window, rather than in new windows.
  • Help you work faster and easily stand out from the crowd! One second to switch between dozens of open documents!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    ilhan · 4 days ago
    peki 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.
    MC · 28 days ago
    this code will not work because there are two many permutations


    should be:

    this code will not work because there are too many permutations


    HTH
    • To post as a guest, your comment is unpublished.
      skyyang · 26 days ago
      Hello, MC,
      Thank you for your warm reminder, it is my mistake. I have corrected it.
      Thanks a lot!
  • To post as a guest, your comment is unpublished.
    Paul · 11 months ago
    Hello 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. pauladah69@gmail.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.
    Wule · 11 months ago
    How many sequences of 3things can be formed from 7 different things replacement and order is important?
  • To post as a guest, your comment is unpublished.
    Declan Keogh · 1 years ago
    @Supraja...

    in the first sub clear all cells... not just the first row
    --Cells.Clear

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