Microsoft 365 Userform faire 2 combobox en cascade + 1 listbox en fonction des combobox

Superemi

XLDnaute Junior
Bonjour à tous, le forum,

Je souhaite faire 1 combobox sur ma 1ère colonne sans doublon, puis en fonction de mon choix faire la 2ème combobox sur ma 2ème colonne.
Sur la Listbox apparait les critères correspondant à mes choix précédents.

Je me suis inspiré d'1 fichier trouvé sur internet, mais je n'arrive pas à alimenter ma 1ère combobox sur ma 1ère colonne (elle prend en compte les en-têtes de colonne).

1 fichier valant mieux que des mots, vous le trouverez en PJ avec les explications.

Par avance merci de votre aide.
Superemi
 

Pièces jointes

  • Combobox + listbox.xlsm
    73.4 KB · Affichages: 18

mapomme

XLDnaute Barbatruc
Supporter XLD
Bonjour @Superemi :) ,

Un essai : voir le code associé à userform1 dans le fichier joint :
VB:
Option Explicit
Dim Nop As Boolean

Private Sub UserForm_Initialize()
Dim t, i&
    Nop = True
    t = Sheets("Listes").ListObjects(1).ListColumns(1).Range
    For i = 2 To UBound(t)
        ComboChoixColFiltre.Text = t(i, 1)
        If ComboChoixColFiltre.ListIndex = -1 Then ComboChoixColFiltre.AddItem t(i, 1)
    Next i
    ComboChoixColFiltre.Text = ""
    ListBox1.Clear
End Sub

Private Sub ComboChoixColFiltre_click()
Dim t, ref, i&
    Nop = True
    t = Sheets("Listes").ListObjects(1).ListColumns(1).Range.Resize(, 2)
    ComboBoxRech.Clear
    If ComboChoixColFiltre.ListIndex = -1 Then Exit Sub
    ref = ComboChoixColFiltre.Text
    For i = 2 To UBound(t)
        If ref = t(i, 1) Then
            ComboBoxRech.Text = t(i, 2)
            If ComboBoxRech.ListIndex = -1 Then ComboBoxRech.AddItem t(i, 2)
        End If
    Next i
    ComboBoxRech.Text = ""
    ListBox1.Clear
    Nop = False
End Sub

Private Sub ComboBoxRech_Change()
Dim ref1, ref2, t, i&, j&
    If Nop Then Exit Sub
    ListBox1.Clear
    ListBox1.ColumnCount = 5
    ListBox1.ColumnWidths = "150;40;40;40"
    If ComboBoxRech.ListIndex = -1 Then Exit Sub
    ref1 = ComboChoixColFiltre.Text: ref2 = ComboBoxRech.Text
    t = Sheets("Listes").ListObjects(1).ListColumns(1).Range.Resize(, 4)
    For i = 2 To UBound(t)
        If t(i, 1) = ref1 And t(i, 2) = ref2 Then
            ListBox1.AddItem t(i, 1)
            For j = 2 To 4: ListBox1.List(ListBox1.ListCount - 1, j - 1) = t(i, j): Next
        End If
    Next i
End Sub
 

Pièces jointes

  • Superemi- liste cascade- v1.xlsm
    34.6 KB · Affichages: 24

Superemi

XLDnaute Junior
Bonjour mapomme, ChTi160,

Vous avez été très rapide alors que ma réponse est tardive !
Après réflexion, j'ai retenu l'option de ChTi160 qui a l'option de tout afficher.

J'ai pu adapter sur mon fichier c'est parfait, merci à vous !

Cordialement,
Superemi
 

Discussions similaires

Réponses
21
Affichages
1 K

Statistiques des forums

Discussions
312 109
Messages
2 085 381
Membres
102 876
dernier inscrit
BouteilleMan