Vai al contenuto principale

Come generare o elencare tutte le possibili permutazioni in Excel?

Ad esempio, ho tre caratteri XYZ, ora, voglio elencare tutte le possibili permutazioni basate su questi tre caratteri per ottenere sei risultati diversi come questo: XYZ, XZY, YXZ, YZX, ZXY e ZYX. In Excel, come potresti generare o elencare rapidamente tutte le permutazioni in base a un numero diverso di caratteri?

Genera o elenca tutte le possibili permutazioni in base a caratteri con codice VBA


freccia blu freccia destra Genera o elenca tutte le possibili permutazioni in base a caratteri con codice VBA

Il seguente codice VBA può aiutarti a elencare tutte le permutazioni in base al tuo numero specifico di lettere, per favore fai come segue:

1. Tieni premuto il ALT + F11 chiavi per aprire il Microsoft Visual Basic, Applications Edition finestra.

2. Clic inserire > Modulie incolla il codice seguente nel file Moduli Finestra.

Codice VBA: elenca tutte le possibili permutazioni 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. Quindi premere F5 chiave per eseguire questo codice e viene visualizzata una finestra di messaggio per ricordarti di inserire i caratteri che desideri elencare tutte le permutazioni, vedi screenshot:

permutazioni elenco doc 1

4. Dopo aver inserito i caratteri, quindi fare clic su OK pulsante, tutte le possibili permutazioni vengono visualizzate nella colonna A del foglio di lavoro attivo. Vedi screenshot:

permutazioni elenco doc 2

Note:: Se la lunghezza dei caratteri immessi è uguale o superiore a 8 caratteri, questo codice non funzionerà perché ci sono troppe permutazioni.

permutazioni elenco doc 3


Elenca o genera tutte le possibili combinazioni da più colonne

Se è necessario generare tutte le possibili combinazioni in base ai dati di più colonne, forse, non esiste un buon modo per gestire l'attività. Ma, Kutools for Excel's Elenca tutte le combinazioni utility può aiutarti a elencare tutte le possibili combinazioni in modo rapido e semplice. Fai clic per scaricare Kutools per Excel!

doc elenca tutte le combinazioni

Kutools for Excel: con più di 300 utili componenti aggiuntivi di Excel, liberi di provare senza limitazioni in 30 giorni. Scarica e prova gratuita ora!

I migliori strumenti per la produttività in ufficio

🤖 Assistente AI di Kutools: Rivoluziona l'analisi dei dati basandosi su: Esecuzione intelligente   |  Genera codice  |  Crea formule personalizzate  |  Analizzare i dati e generare grafici  |  Richiama le funzioni di Kutools...
Funzioni popolari: Trova, evidenzia o identifica i duplicati   |  Elimina righe vuote   |  Combina colonne o celle senza perdere dati   |   Round senza formula ...
Super ricerca: VLookup a criteri multipli    VLookup a valori multipli  |   VLookup su più fogli   |   Ricerca fuzzy ....
Elenco a discesa avanzato: Crea rapidamente un elenco a discesa   |  Elenco a discesa dipendente   |  Elenco a discesa a selezione multipla ....
Gestore di colonna: Aggiungi un numero specifico di colonne  |  Sposta colonne  |  Attiva/disattiva lo stato di visibilità delle colonne nascoste  |  Confronta intervalli e colonne ...
Funzionalità in primo piano: Messa a fuoco della griglia   |  Vista di progettazione   |   Grande barra delle formule    Gestore di cartelle di lavoro e fogli   |  Resource Library (Testo automatico)   |  Date picker   |  Combina fogli di lavoro   |  Crittografa/decrittografa le celle    Invia e-mail per elenco   |  Super filtro   |   Filtro speciale (filtro grassetto/corsivo/barrato...) ...
I 15 migliori set di strumenti12 Testo Strumenti (aggiungi testo, Rimuovi personaggi, ...)   |   50+ Grafico Tipi (Diagramma di Gantt, ...)   |   40+ Pratico Formule (Calcola l'età in base al compleanno, ...)   |   19 Inserimento Strumenti (Inserisci il codice QR, Inserisci immagine dal percorso, ...)   |   12 Conversione Strumenti (Numeri in parole, Conversione di valuta, ...)   |   7 Unisci e dividi Strumenti (Combina righe avanzate, Celle divise, ...)   |   ... e altro ancora

Potenzia le tue competenze di Excel con Kutools per Excel e sperimenta l'efficienza come mai prima d'ora. Kutools per Excel offre oltre 300 funzionalità avanzate per aumentare la produttività e risparmiare tempo.  Fai clic qui per ottenere la funzionalità di cui hai più bisogno...

Descrizione


Office Tab porta l'interfaccia a schede in Office e semplifica notevolmente il tuo lavoro

  • Abilita la modifica e la lettura a schede in Word, Excel, PowerPoint, Publisher, Access, Visio e Project.
  • Apri e crea più documenti in nuove schede della stessa finestra, piuttosto che in nuove finestre.
  • Aumenta la produttività del 50% e riduce ogni giorno centinaia di clic del mouse!
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