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 94 95 96 97 98 99
| Option Explicit
Dim Ws As Worksheet
Dim NbLignes As Integer
Dim k As String
Dim i As Integer
Dim LaDerniere As String
Private Sub CommandButton1_Click()
LaDerniere = Application.Workbooks("CascadeCombobox.xls").Worksheets("Base").Cells(i, 4000).End(xlUp).Row
k = 1
For i = 2 To LaDerniere
If Application.Workbooks("CascadeCombobox.xls").Worksheets("Base").Cells(i, 1).Value = ComboBox1.Value and Application.Workbooks("CascadeCombobox.xls").Worksheets("Base").Cells(i, 2).Value = ComboBox2.Value and Application.Workbooks("CascadeCombobox.xls").Worksheets("Base").Cells(i, 3).Value = ComboBox3.Value Then
Application.Workbooks("CascadeCombobox.xls").Worksheets("Base").Range("A" & i & ":E" & i & "").Copy
Application.Workbooks("CascadeCombobox.xls").Worksheets("Base").Range("F" & k & "").Select
ActiveSheet.Paste
k = k + 1
End If
Next i
End Sub
'Les données sont dans les colonnes A à D, d'un onglet nommé "Base".
'La procédure effectue un remplissage conditionnel des Combobox en fonction de
'ce qui est sélectionnée dans le contrôle précédent:
'La sélection du ComboBox1 (données colonne A) définit le contenu du ComboBox2 (données colonne B) ,
'La sélection du ComboBox2 définit le contenu du ComboBox3 (données colonne C)
etc...
'
Private Sub UserForm_Initialize()
'Définit la feuille contenant les données
Set Ws = Worksheets("Base")
'Définit le nombre de lignes dans la colonne A
NbLignes = Ws.Range("A65536").End(xlUp).Row
'Remplissage du ComboBox1
Alim_Combo 1
End Sub
Private Sub ComboBox1_Change()
'Remplissage Combo2
Alim_Combo 2, ComboBox1.Value
End Sub
Private Sub ComboBox2_Change()
'Remplissage Combo3
Alim_Combo 3, ComboBox2.Value
End Sub
Private Sub ComboBox3_Change()
'Remplissage Combo4
Alim_Combo 4, ComboBox3.Value
End Sub
'Procédure pour alimenter les ComboBox
Private Sub Alim_Combo(CbxIndex As Integer, Optional Cible As Variant)
Dim j As Integer
Dim Obj As Control
'Définit le ComboBox à remplir
Set Obj = Me.Controls("ComboBox" & CbxIndex)
'Supprime les anciennes données
Obj.Clear
'alimente le Combobox initial (Combobox1)
If CbxIndex = 1 Then
'Boucle sur les lignes de la colonne A (à partir de la 2eme ligne)
For j = 2 To NbLignes
Obj = Ws.Range("A" & j)
'Remplit le ComboBox sans doublons
If Obj.ListIndex = -1 Then Obj.AddItem Ws.Range("A" & j)
Next j
Else
'Alimentation conditionnelle des autres Combobox en fonction de
'ce qui est sélectionnée dans le contrôle précédent:
'(La sélection du ComboBox1 définit le contenu du ComboBox2,
'La sélection du ComboBox2 définit le contenu du ComboBox3
etc...)
For j = 2 To NbLignes
If Ws.Range("A" & j).Offset(0, CbxIndex - 2) = Cible Then
Obj = Ws.Range("A" & j).Offset(0, CbxIndex - 1)
If Obj.ListIndex = -1 Then Obj.AddItem Ws.Range("A" & j).Offset(0, CbxIndex - 1)
End If
Next j
End If
'Enlève la sélection dans le ComboBox
Obj.ListIndex = -1
End Sub |
Partager