permutations

ANANKE

New Member
Joined
Feb 12, 2013
Messages
8
Hi,

I have 7 elements in A called a b c d e f g and I would need to have all the possible permutations (7!) in excel.
The strings should not contain repetitions of elements.

Can somebody help me?

Thanks for all suggestions
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Try this, I believe the author of the code "GetPermutation(X As String, y As String)" is unknown, per MVP's of the old MS Exel Users Group.
Does the abcdefg string's 5040 Permutations in a split second.

Code:
Option Explicit
Option Compare Text
Dim CurrentRow
Sub DoString()
On Error Resume Next
    Dim Instring As String
    Dim i As Integer, j As Integer
    Instring = Range("A1").Value
        Range("A1").Select
        CurrentRow = 1
    Call GetPermutation("", Instring)
End Sub

Sub GetPermutation(X As String, y As String)
On Error Resume Next
Dim j, i
j = Len(y)
    If j < 2 Then
            Cells(CurrentRow, 1) = X & y
            CurrentRow = CurrentRow + 1
      Else
        For i = 1 To j
            Call GetPermutation(X + Mid(y, i, 1), _
            Left(y, i - 1) + Right(y, j - i))
        Next
    End If
 
End Sub

Regards,
Howard
 
Upvote 0
thats exactly what I was looking for

thanks a lot


Try this, I believe the author of the code "GetPermutation(X As String, y As String)" is unknown, per MVP's of the old MS Exel Users Group.
Does the abcdefg string's 5040 Permutations in a split second.

Code:
Option Explicit
Option Compare Text
Dim CurrentRow
Sub DoString()
On Error Resume Next
    Dim Instring As String
    Dim i As Integer, j As Integer
    Instring = Range("A1").Value
        Range("A1").Select
        CurrentRow = 1
    Call GetPermutation("", Instring)
End Sub

Sub GetPermutation(X As String, y As String)
On Error Resume Next
Dim j, i
j = Len(y)
    If j < 2 Then
            Cells(CurrentRow, 1) = X & y
            CurrentRow = CurrentRow + 1
      Else
        For i = 1 To j
            Call GetPermutation(X + Mid(y, i, 1), _
            Left(y, i - 1) + Right(y, j - i))
        Next
    End If
 
End Sub

Regards,
Howard
 
Upvote 0
Lhoward,

Thank you very much for sharing - one for my archives - thanks.


West Man,

Sample raw data:


Excel 2007
A
1abcdefg
2
3
4
5
6
7
8
9
10
11
Sheet1


After the macro:


Excel 2007
A
1abcdefg
2abcdegf
3abcdfeg
4abcdfge
5abcdgef
5035gfedabc
5036gfedacb
5037gfedbac
5038gfedbca
5039gfedcab
5040gfedcba
5041
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Option Explicit
Option Compare Text
Dim CurrentRow

Sub DoString()
'' I believe the author of the code "GetPermutation(X As String, y As String)"
''   is unknown, per MVP's of the old MS Exel Users Group.
'' Does the abcdefg string's 5040 Permutations in a split second.
On Error Resume Next
Dim Instring As String
Dim i As Integer, j As Integer
Instring = Range("A1").Value
Range("A1").Select
CurrentRow = 1
Call GetPermutation("", Instring)
End Sub

Sub GetPermutation(X As String, y As String)
On Error Resume Next
Dim j, i
j = Len(y)
If j < 2 Then
  Cells(CurrentRow, 1) = X & y
  CurrentRow = CurrentRow + 1
Else
  For i = 1 To j
    Call GetPermutation(X + Mid(y, i, 1), Left(y, i - 1) + Right(y, j - i))
  Next
End If
End Sub


Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the DoString macro.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,348
Members
452,638
Latest member
Oluwabukunmi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top