Bonjour,

Je souhaiterai un coup de main pour créer le code d'un USERFORM pour gérer une liste intuitive, en remplacement d'un liste de choix déroulante dépendant d'une autre qui elle même dépend d'une autre et d'une autre et d'une autre...

Voici le USERFORM de travail:
Nom : Userform.jpg
Affichages : 149
Taille : 48,7 Ko

Le choix du "Libelle_1" donne accès au "Libelle_2", qui donne accès au "Libelle_3", etc jusqu'a "Prix".

J'ai commencé à travaillé le code, mais je suis totalement incompétent (en un seul mot) dans ce domaine, et je rame comme pas permis.

Voici mon début de code (que j'ai trouvé au cour de recherches, et commencé à modifié:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
 
Dim choix1(), choix2(), choix3(), choix4(), TblChoix4()
 
Private Sub Label1_Click()
 
End Sub
 
Private Sub Label4_Click()
 
End Sub
 
Private Sub UserForm_Initialize()
  choix1 = Application.Transpose(Range("Libelle_1"))
  choix2 = Application.Transpose(Range("Libelle_2"))
  choix3 = Application.Transpose(Range("Libelle_3").Value)
  choix4 = Application.Transpose(Range("Unite").Value)
  Set d1 = CreateObject("Scripting.Dictionary")
  For Each c In choix1
    d1(c) = ""
  Next c
  Me.ComboBox1.List = d1.keys
End Sub
Private Sub ComboBox1_Change()
   If Me.ComboBox1.ListIndex = -1 And IsError(Application.Match(Me.ComboBox1, choix1, 0)) Then
     Set d1 = CreateObject("Scripting.Dictionary")
     tmp = UCase(Me.ComboBox1) & "*"
     For Each c In choix1
       If UCase(c) Like tmp Then d1(c) = ""
     Next c
     Me.ComboBox1.List = d1.keys
     Me.ComboBox1.DropDown
   Else
     Condition = UCase(Me.ComboBox1)
     If Condition = "" Then Exit Sub
     ligne = 0
     ReDim TblChoix2(1 To UBound(choix2))
     For i = LBound(choix2) To UBound(choix2)
       If choix1(i) = Condition Then ligne = ligne + 1: TblChoix2(ligne) = choix2(i)
     Next i
 
     ' Choix 2
      If Me.ComboBox2.ListIndex = -1 And IsError(Application.Match(Me.ComboBox2, choix2, 0)) Then
     Set d1 = CreateObject("Scripting.Dictionary")
     tmp = UCase(Me.ComboBox2) & "*"
     For Each c In choix2
       If UCase(c) Like tmp Then d1(c) = ""
     Next c
     Me.ComboBox2.List = d1.keys
     Me.ComboBox2.DropDown
   Else
     Condition = UCase(Me.ComboBox2)
     If Condition = "" Then Exit Sub
     ligne = 0
     ReDim TblChoix3(1 To UBound(choix2))
     For i = LBound(choix3) To UBound(choix3)
       If choix2(i) = Condition Then ligne = ligne + 1: TblChoix3(ligne) = choix3(i)
     Next i
     End If
     ' Fin choix 2
 
     ReDim Preserve TblChoix3(1 To ligne)
     Me.ComboBox2.List = TblChoix3
     Me.ComboBox2.SetFocus
     If Val(Application.Version) > 10 Then SendKeys "{f4}"
   End If
End Sub
Private Sub ComboBox2_Change()
If Me.ComboBox1 <> "" Then
  If Me.ComboBox2.ListIndex = -1 Then
   Set d1 = CreateObject("Scripting.Dictionary")
   tmp = UCase(Me.ComboBox2) & "*"
   For Each c In TblChoix2
     If UCase(c) Like tmp Then d1(c) = ""
   Next c
   Me.ComboBox2.List = d1.keys
   Me.ComboBox2.DropDown
  Else
   tmp = Application.Match(Me.ComboBox2, [Ville], 0)
   Me.TextBox1 = Range("cp")(tmp)
  End If
 End If
End Sub
Private Sub CommandButton1_Click()
 If Me.ComboBox1 <> "" And Me.ComboBox2 <> "" Then
  ActiveCell = UCase(Me.ComboBox1)
  ActiveCell.Offset(, 1) = Me.ComboBox2
  ActiveCell.Offset(, 2) = Me.TextBox1
  Unload Me
 Else
   MsgBox "Incomplet!"
   Exit Sub
 End If
End Sub
Bien sûr, ce code est incomplet, et lors de l'utilsation de l'outil, j'ai le message Erreur d'exécution '9': L'indice n'appartient pas à la sélection
Lorsque je vais au débogage, cette ligne est surligné en jaune:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
     ReDim Preserve TblChoix3(1 To ligne)
Si toutefois une âme charitable pouvait m'aider sur ce projet personnel, qui me permettra de générer des factures rapidement, ce serai vraiment sympathique.
Merci.