Combobox en cascade sur titre de colonne

msingle

XLDnaute Junior
Bonjour à tous,

Je cherche à récupérer dans une combobox les titres de colonnes en fonction de la valeur d'une première combobox.

Dans le fichier joint, je voudrais donc que dans la combobox Taille, ne s'affichent que les tailles existantes en fonction de la marque choisie.

Pouvez-vous m'aider?
Marc
 

Pièces jointes

  • Combo_Cascade.xls
    38.5 KB · Affichages: 78

kjin

XLDnaute Barbatruc
Re : Combobox en cascade sur titre de colonne

Bonjour,
Rajoute dans le module
Code:
Private Sub ComboBox2_Change()
Dim L%, i%
If ComboBox2.ListIndex = -1 Then Exit Sub
L = ComboBox2.ListIndex + 2
ComboBox3.Clear
For i = 2 To 5
    If UCase(Cells(L, i)) = "X" Then ComboBox3.AddItem Cells(1, i)
Next
End Sub
A+
kjin
 

kjin

XLDnaute Barbatruc
Re : Combobox en cascade sur titre de colonne

Re,
Note que dans ton cas (pas de doublons dans la colonne A)...
Code:
Private Sub UserForm_Initialize()
With Sheets("DB")
    ComboBox2.List = .Range([a2], [a65000].End(xlUp)).Value
End With
End Sub
...suffit
A+
kjin
 

msingle

XLDnaute Junior
Re : Combobox en cascade sur titre de colonne

@kijn
Effectivement c'est plus simple!

Mais mon problème n'est pas tout à fait résolu car mon fichier réel comporte des lignes vierges (voir nouveau fichier), et donc cela ne fonctionne pas...:(

Que faut-il modifier?
Marc
 

Pièces jointes

  • Combo_Cascade2.xls
    39 KB · Affichages: 78

kjin

XLDnaute Barbatruc
Re : Combobox en cascade sur titre de colonne

Bonsoir, re Fo_rum :),
J'ai fais plus simple :D
Code:
Private Sub ComboBox2_Change()
Dim L%, i%
If ComboBox2.ListIndex = -1 Then Exit Sub
L = ComboBox2.List(ComboBox2.ListIndex, 1)
ComboBox3.Clear
For i = 2 To 5
    If UCase(Cells(L, i)) = "X" Then ComboBox3.AddItem Cells(1, i)
Next
End Sub

Private Sub UserForm_Initialize()
Dim T, i%
With Sheets("DB")
    T = .Range([a2], [a65000].End(xlUp)).Value
End With
With ComboBox2
.ColumnCount = 2
.ColumnWidths = .Width & ";" & 0
    For i = 1 To UBound(T)
        If Not IsEmpty(T(i, 1)) Then
            .AddItem T(i, 1)
            .List(.ListCount - 1, 1) = i + 1
        End If
    Next
End With
End Sub
A+
kjin
 

msingle

XLDnaute Junior
Re : Combobox en cascade sur titre de colonne

@kijn
J'ai appliqué le code simplifié (bravo!) en l'adaptant au fichier réel de la manière suivante :
Private Sub ComboBox2_Change()
Dim L%, i%
If ComboBox2.ListIndex = -1 Then Exit Sub
L = ComboBox2.List(ComboBox2.ListIndex, 1)
ComboBox3.Clear
For i = 59 To 62
If UCase(Cells(L, i)) <> "" Then ComboBox3.AddItem Cells(2, i)
Next
End Sub
Private Sub UserForm_Initialize()
Dim T, i%
With Sheets("DATABASE")
T = .Range([bf2], [bf65000].End(xlUp)).Value
End With
With ComboBox2
.ColumnCount = 2
.ColumnWidths = .Width & ";" & 0
For i = 2 To UBound(T)
If Not IsEmpty(T(i, 1)) Then
.AddItem T(i, 1)
.List(.ListCount - 1, 1) = i + 1
End If
Next
End With
End Sub

Et j'ai l'erreur suivante :
"Erreur d'exécution 1004
Erreur définie par l'application ou par l'objet" ????

Help!

PS: pour info, la première ligne de ma feuille DATABASE est vide, et les données sont dans les colonnes BF pour les marques et entre BG et BJ pour les tailles.
 

kjin

XLDnaute Barbatruc
Re : Combobox en cascade sur titre de colonne

Bonsoir,
Essaies comme ceci
Code:
Private Sub ComboBox2_Change()
Dim L%, i%
If ComboBox2.ListIndex = -1 Then Exit Sub
L = ComboBox2.List(ComboBox2.ListIndex, 1)
ComboBox3.Clear
With Sheets("DATABASE")
    For i = 59 To 62
        If UCase(.Cells(L, i)) <> "" Then ComboBox3.AddItem .Cells(2, i)
    Next
End With
End Sub

Private Sub UserForm_Initialize()
Dim T, i%
With Sheets("DATABASE")
    T = .Range("BF2:BF" & .Range("BF65000").End(xlUp).Row).Value
End With
With ComboBox2
    .ColumnCount = 2
    .ColumnWidths = .Width & ";" & 0
    For i = 2 To UBound(T)
        If Not IsEmpty(T(i, 1)) Then
            .AddItem T(i, 1)
            .List(.ListCount - 1, 1) = i + 1
        End If
    Next
End With
End Sub
A+
kjin
 

Fo_rum

XLDnaute Accro
Re : Combobox en cascade sur titre de colonne

Re

@msingle : merci pour les remerciements.
@kjin, plus court
Code:
Dim Dli As Long, Li As Long, i As Long, C As Byte
Private Sub UserForm_Initialize()
  With Sheets("DATABASE")
    Dli = .Cells(Rows.Count, "BF").End(xlUp).Row
    For Li = 2 To Dli
      If [B][COLOR="Red"].[/COLOR][/B]Cells(Li, "BF") <> "" Then ComboBox2.AddItem [COLOR="red"][B].[/B][/COLOR]Cells(Li, "BF")
    Next
  End With
End Sub
Private Sub ComboBox2_Change()
  Dim Est As Range
  ComboBox3.Clear
  With Sheets("DATABASE")
    Set Est = .Range("BF2:BF" & Dli).Find(ComboBox2)
    For C = 59 To 62
      If .Cells(Est.Row, C) <> "" Then ComboBox3.AddItem .Cells(2, C)
    Next
  End With
End Sub
 
Dernière édition:

Discussions similaires

Réponses
28
Affichages
1 K

Statistiques des forums

Discussions
312 216
Messages
2 086 344
Membres
103 194
dernier inscrit
rtison