Sub test()
MsgBox GenerateCode(5, 3) '5 lettres et 3 chiffres
MsgBox GenerateCode(8, 3, 4) '8 lettres et 3 chiffres ,4 caractères spé
MsgBox GenerateCode(3, 5, 2) '3 lettres 5 ch<iffres,3 caractères spé
MsgBox GenerateCode(3, 5, 2, True) '3lettres 5 chiffres,2 caractères spé le tout mélangé
MsgBox GenerateCode(3, 5, 4, True) '3lettres 5 chiffres,4 caractères spé le tout mélangé
End Sub
Function GenerateCode(Optional NbChar& = 0, Optional NbNum& = 0, Optional nbCharSpé As Long = 0, Optional Melange = False)
Dim Y&, C&, L, X&, Temp, I&
If NbChar = 0 Then NbChar = 2 + (Round(Rnd * 6))
If NbNum = 0 Then NbNum = 2 + (Round(Rnd * 3))
Set Dico = CreateObject("Scripting.Dictionary")
StrLettres = Split(StrConv("abcdefghijklmnopqrstuvwxyzABCDEFG<HIJKLMNOPQRSTUVWXYZ", vbUnicode), Chr(0))
StrChiffres = Split(StrConv("0123456789", vbUnicode), Chr(0))
StrCharSpé = Split(StrConv("></-+;\#!:,?()", vbUnicode), Chr(0))
Do While Dico.Count < NbNum
Y = Round(Rnd * UBound(StrChiffres))
Dico(StrChiffres(Y)) = ""
Loop
Do While Dico.Count < NbChar + NbNum
Y = Round(Rnd * UBound(StrLettres))
Dico(StrLettres(Y)) = ""
Loop
Do While Dico.Count < NbChar + NbNum + nbCharSpé
Y = Round(Rnd * UBound(StrCharSpé))
Dico(StrCharSpé(Y)) = ""
Loop
L = Dico.Keys
Randomize
If Melange Then
For I = LBound(L) To UBound(L)
X = Round(UBound(L) * Rnd)
Temp = L(I): L(I) = L(X): L(X) = Temp
Next
End If
GenerateCode = Join(L, "")
End Function