Combinaison de caractère
Bonjour,
Je me casse la tête depuis un petit moment sur un truc qui pourtant ne doit pas être si compliqué...
Voilà J'ai un fichier avec 4 colonnes dans chaque de ces colonnes il y a des lettres et la concaténation de ces 4 colonnes donne un code ex :
Colonne A Colonne B Colonne C Colonne D
Q D A F
X H P C
Les combinaison de ces 4 colonnes me donne un code ex : QHAC, XDPF,QDPF, etc ... Je voudrais donc créer une fonction par exemple qui me permettent d'afficher toutes les combinaisons possible... mais pas moyen
Voilà ce que j'ai fais pour le moment, mais ça ne donne pas de résultats :
Function combitest(Plage1 As Range, Plage2 As Range, Plage3 As Range, Plage4 As Range, Indic As Long)
Dim i, j, k, l, m As Long, T_Out()
Dim Tabl1(), Tabl2(), Tabl3(), Tabl4() As Range
Dim Plag1, Plag2, Plag3, Plag4 As Range
Set Plag1 = Range(Plage1.Address)
Set Plag2 = Range(Plage2.Address)
Set Plag3 = Range(Plage3.Address)
Set Plag4 = Range(Plage4.Address)
Tabl1 = Plag1
Tabl2 = Plag2
Tabl3 = Plag3
Tabl4 = Plag4
For i = LBound(Tabl1) To UBound(Tabl1)
For j = LBound(Tabl2) To UBound(Tabl2)
For k = LBound(Tabl3) To UBound(Tabl3)
For l = LBound(Tabl4) To UBound(Tabl4)
ReDim Preserve T_Out(m)
T_Out(m) = Tabl1(i, 1) & Tabl2(j, 1) & Tabl3(k, 1) & Tabl4(l, 1)
m = m + 1
Next
Next
Next
Next
combitest = T_Out(Indic)
End FunctionBonjour et bienvenue sur le forum
Un essai à tester. Te convient-il ?
Bye !
Bonjour,
Merci pour ta réponse et ton accueil
J'avais trouvé sur le net une fonction qui permet de le faire pour 2 colonnes, du coup j'avais essayé de l'adapter pour 4 Voilà le code :
Function WindflyCombinaisons(Plage1 As Range, Plage2 As Range, Indic As Long)
Dim Tabl(), Tabl2(), Plag1 As Range, Plag2 As Range, i As Long, j As Long, k As Long, T_Out()
Set Plag1 = Range(Plage1.Address)
Set Plag2 = Range(Plage2.Address)
Tabl = Plag1
Tabl2 = Plag2
For i = LBound(Tabl) To UBound(Tabl)
For j = LBound(Tabl2) To UBound(Tabl2)
ReDim Preserve T_Out(k)
T_Out(k) = Tabl(i, 1) & Tabl2(j, 1)
k = k + 1
Next
Next
WindflyCombinaisons = T_Out(Indic)
End FunctionEdit : J'ai une autre contrainte aussi je n'ai pas forcément le meme nombre de ligne dans ma colonne 1 et ma colonne 2 par exemple
Je pense avoir réussi !
Private Sub CommandButton1_Click()
Dim Compto, liste(), listeCodes()
Dim a&, b&, c&, d&, k&
Dim Compt1, Compt2, Compt3, Compt4
Dim Plage1, Plage2, Plage3, Plage4, Plage5 As Range
Dim Plag1, Plag2, Plag3, Plag4, Plag5
Dim Tabl1(), Tabl2(), Tabl3(), Tabl4(), Tabl5() As Range
Compt1 = Range("A" & Rows.Count).End(xlUp).Row
Compt2 = Range("B" & Rows.Count).End(xlUp).Row
Compt3 = Range("C" & Rows.Count).End(xlUp).Row
Compt4 = Range("D" & Rows.Count).End(xlUp).Row
Plage1 = Range("A1:A" & Compt1)
Plage2 = Range("B1:B" & Compt2)
Plage3 = Range("C1:C" & Compt3)
Plage4 = Range("D1:D" & Compt4)
Tabl1 = Plage1
Tabl2 = Plage2
Tabl3 = Plage3
Tabl4 = Plage4
k = 0
For a = 2 To Compt1
For b = 2 To Compt2
For c = 2 To Compt3
For d = 2 To Compt4
ReDim Preserve listeCodes(1 To 4, 1 To k + 1)
listeCodes(1, k + 1) = Tabl1(a, 1) & Tabl2(b, 1) & Tabl3(c, 1) & Tabl4(d, 1)
k = k + 1
Next d
Next c
Next b
Next a
Range("E2").Resize(UBound(listeCodes, 2), 1) = Application.Transpose(listeCodes)
End Subce n'est peut-être pas le plus "optimisé", mais ça à le mérite de fonctionner