Salt la conținutul principal

Cum se generează sau se enumeră toate permutările posibile în Excel?

De exemplu, am trei caractere XYZ, acum, vreau să listez toate permutările posibile pe baza acestor trei caractere pentru a obține șase rezultate diferite astfel: XYZ, XZY, YXZ, YZX, ZXY și ZYX. În Excel, cum ați putea genera sau lista rapid toate permutările pe baza unui număr diferit de caractere?

Generați sau enumerați toate permutările posibile pe baza caracterelor cu cod VBA


săgeată albastru dreapta balon Generați sau enumerați toate permutările posibile pe baza caracterelor cu cod VBA

Următorul cod VBA vă poate ajuta să listați toate permutările pe baza numărului dvs. specific de litere, vă rugăm să procedați după cum urmează:

1. Țineți apăsat butonul ALT + F11 tastele pentru a deschide Microsoft Visual Basic pentru aplicații fereastră.

2. Clic Insera > Moduleși lipiți următorul cod în Module Fereastră.

Cod VBA: enumerați toate permutările posibile în 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. Apoi apăsați F5 pentru a rula acest cod și apare o casetă de prompt pentru a vă reaminti să introduceți caracterele pe care doriți să le afișați toate permutările, a se vedea captura de ecran:

permutări listă doc. 1

4. După introducerea caracterelor, apoi faceți clic pe OK butonul, toate permutările posibile sunt afișate în coloana A a foii de lucru active. Vedeți captura de ecran:

permutări listă doc. 2

notițe: Dacă lungimea caracterului introdus este egală sau mai mare de 8 caractere, acest cod nu va funcționa deoarece există prea multe permutări.

permutări listă doc. 3


Enumerați sau generați toate combinațiile posibile din mai multe coloane

Dacă trebuie să generați toate combinațiile posibile pe baza datelor cu mai multe coloane, poate că nu există o modalitate bună de a face față sarcinii. Dar, Kutools pentru Excel's Enumerați toate combinațiile utilitarul vă poate ajuta să listați toate combinațiile posibile rapid și ușor. Faceți clic pentru a descărca Kutools pentru Excel!

doc enumeră toate combinațiile

Kutools pentru Excel: cu mai mult de 300 de programe de completare Excel la îndemână, încercați fără limitări în 30 de zile. Descărcați și proba gratuită acum!

Cele mai bune instrumente de productivitate de birou

🤖 Kutools AI Aide: Revoluționați analiza datelor pe baza: Execuție inteligentă   |  Generați codul  |  Creați formule personalizate  |  Analizați datele și generați diagrame  |  Invocați funcțiile Kutools...
Caracteristici populare: Găsiți, evidențiați sau identificați duplicatele   |  Ștergeți rândurile goale   |  Combinați coloane sau celule fără a pierde date   |   Rundă fără Formula ...
Super căutare: VLookup cu mai multe criterii    VLookup cu valori multiple  |   VLookup pe mai multe foi   |   Căutare fuzzy ....
Listă derulantă avansată: Creați rapid o listă derulantă   |  Listă drop-down dependentă   |  Listă derulantă cu selectare multiplă ....
Manager de coloane: Adăugați un număr specific de coloane  |  Mutați coloanele  |  Comutați starea vizibilității coloanelor ascunse  |  Comparați intervale și coloane ...
Caracteristici prezentate: Focus pe grilă   |  Vedere de proiectare   |   Big Formula Bar    Manager registru de lucru și foi   |  Biblioteca de resurse (Text automat)   |  Data Picker   |  Combinați foi de lucru   |  Criptare/Decriptare celule    Trimiteți e-mailuri după listă   |  Super Filtru   |   Filtru special (filtrează bold/italic/barat...) ...
Top 15 seturi de instrumente12 Text Instrumente (Adăuga text, Eliminați caractere,...)   |   50+ Diagramă Tipuri de (Gantt Chart,...)   |   40+ Practic Formule (Calculați vârsta pe baza zilei de naștere,...)   |   19 inserare Instrumente (Introduceți codul QR, Inserați imaginea din cale,...)   |   12 Convertire Instrumente (Numere la cuvinte, conversie valutara,...)   |   7 Merge & Split Instrumente (Rânduri combinate avansate, Celule divizate,...)   |   ... și altele

Îmbunătățiți-vă abilitățile Excel cu Kutools pentru Excel și experimentați eficiența ca niciodată. Kutools pentru Excel oferă peste 300 de funcții avansate pentru a crește productivitatea și a economisi timp.  Faceți clic aici pentru a obține funcția de care aveți cea mai mare nevoie...

Descriere


Fila Office aduce interfața cu file în Office și vă face munca mult mai ușoară

  • Activați editarea și citirea cu file în Word, Excel, PowerPoint, Publisher, Access, Visio și Project.
  • Deschideți și creați mai multe documente în filele noi ale aceleiași ferestre, mai degrabă decât în ​​ferestrele noi.
  • Vă crește productivitatea cu 50% și reduce sute de clicuri de mouse pentru dvs. în fiecare zi!
Comments (13)
No ratings yet. Be the first to rate!
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


Please have a try, hope it can help you!
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
This comment was minimized by the moderator on the site
who can send me a list of 10 diferent items permutatted by 2 outcomes. this code doe

snt work on this
This comment was minimized by the moderator on the site
peki bunu listeleyecek bir program uygulama yok mu?basit sıradan bir hesaplamadan daha fazlasına ihtiyacı olan ne yapacak?
This comment was minimized by the moderator on the site
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
This comment was minimized by the moderator on the site
Hello, MC,
Thank you for your warm reminder, it is my mistake. I have corrected it.
Thanks a lot!
This comment was minimized by the moderator on the site
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. .


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
This comment was minimized by the moderator on the site
How many sequences of 3things can be formed from 7 different things replacement and order is important?
This comment was minimized by the moderator on the site
3 to the power of 7: 2187
This comment was minimized by the moderator on the site
@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
This comment was minimized by the moderator on the site
Hello,

Im trying to get a permutation for 82 characters, the code provided works, but, since the columns are only 1048576, I want to move the next output in B,C,D..... Can any of you help me on this regard
This comment was minimized by the moderator on the site
Hello, Im doing a small project using permutation and combination rules. I need your support on this please. Scenario: I have 13 digit alpha numeric data (00SHGO8BJIDG0) I want a coding to interchange S to 5, I to 1 and O to 0 and vise versa. The project is that if I have the correct 13 digit data I will receive a 3 digit pass code. (eg) 00SHG08BJ1DG0 - 500 is the pass code but because of wrong typo that is instead of 1 it was I and 0 it was O there is a wrong info. can you please help me.
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations