[-RESOLUTO-]ComboBox qui depend d'un autre ComboBox

Ilino

XLDnaute Barbatruc
Bonsoir Forum (Buon Anno)
Je souhaite afficher dans le 2eme ComboBox2 une lise selon la sélection du 1 er ComboBox1
Example:
dans le ComboBox1 si je selectionne N1 j'aurais une liste dans le ComboBox2 : A1 et A4
nb: dans le combobox1 : j'ai utilisé un code ( merci bénévole) pour afficher la liste de la colonne C sans doublant
GRAZIE
 

Pièces jointes

  • ILINO COMBOBOX LISTE.xlsm
    18.7 KB · Affichages: 32
  • ILINO COMBOBOX LISTE.xlsm
    18.7 KB · Affichages: 38
  • ILINO COMBOBOX LISTE.xlsm
    18.7 KB · Affichages: 39
Dernière édition:

laetitia90

XLDnaute Barbatruc
Re : ComboBox qui depend d'un autre ComboBox

bonjour Ilino:)

un exemple acceptable!!! sur une petite plage
 

Pièces jointes

  • ILINO COMBOBOX LISTE.xlsm
    24.1 KB · Affichages: 45
  • ILINO COMBOBOX LISTE.xlsm
    24.1 KB · Affichages: 56
  • ILINO COMBOBOX LISTE.xlsm
    24.1 KB · Affichages: 55

Ilino

XLDnaute Barbatruc
Re : ComboBox qui depend d'un autre ComboBox

Bonsoir LAETITIA and buon anno
Merci pour la réponse , mais je ne sais pas pourquoi ça n'a pas fonctionné correctement chez moi
dans le code Initialize j'ai
Code:
Private Sub UserForm_Initialize()

Dim i As Integer 'AFFICHER DANS COMBOBOX SANS DOUBLANT

For i = 1 To 2: Set txt(i).txt = Me("ComboBox" & i): Next i

ComboBox8.Clear
 For i = 2 To Sheets("Contrats").Range("C65536").End(xlUp).Row
   ComboBox8 = Sheets("Contrats").Range("C" & i)
   If ComboBox8.ListIndex = -1 Then ComboBox8.AddItem Sheets("Contrats").Range("C" & i)
 Next i
 
 
 
End Sub
ça n'a rien donné dans le comboBox9
 

job75

XLDnaute Barbatruc
Re : ComboBox qui depend d'un autre ComboBox

Bonsoir Ilino, Laetitia,

Très bonne année à vous.

Fichier joint avec cette solution qui sort un peu du train-train habituel :

Code:
Private Sub Combobox1_Change()
Dim i&, h&
ComboBox2.Clear
If ComboBox1.ListIndex = -1 Then Exit Sub
With Sheets("Listes")
  .UsedRange.Sort .[B1], , , .[A1], xlAscending, Header:=xlYes
  i = Application.Match(ComboBox1, .UsedRange.Columns(2), 0)
  h = Application.CountIf(.UsedRange.Columns(2), ComboBox1)
  ComboBox2.List = .Cells(i, 1).Resize(h, 2).Value 'au moins 2 éléments
End With
End Sub

Private Sub UserForm_Initialize()
With Sheets("Listes")
  .Cells.Clear
  Sheets("Feuil1").[B:C].Copy .[A1]
  .UsedRange.Sort .[B1], Header:=xlYes
  .UsedRange.Columns(3) = "=IF(RC2<>R[1]C2,RC2,"""")"
  .UsedRange = .UsedRange.Value
  .UsedRange.Sort .[C1], xlAscending, Header:=xlYes
  On Error Resume Next
  With .UsedRange.Columns(3).Offset(1).SpecialCells(xlCellTypeConstants)
    ComboBox1.List = .Resize(, 2).Value 'au moins 2 éléments
  End With
End With
End Sub
Bonne nuit et A+
 

Pièces jointes

  • ILINO COMBOBOX LISTE(1).xlsm
    25.5 KB · Affichages: 26
Dernière édition:

job75

XLDnaute Barbatruc
Re : [-RESOLUTO-]ComboBox qui depend d'un autre ComboBox

Bonjour Ilino, Laetitia,

Une solution sans la feuille "Listes", très classique avec le Dictionary et Quick sort :

Code:
Dim tablo 'mémorise

Private Sub Combobox1_Change()
Dim x$, i&, n&, a()
ComboBox2.Clear
If ComboBox1.ListIndex = -1 Then Exit Sub
x = ComboBox1
For i = 1 To UBound(tablo)
  If tablo(i, 2) = x Then
    n = n + 1
    ReDim Preserve a(1 To n)
    a(n) = tablo(i, 1)
  End If
Next
tri a, 1, n
ComboBox2.List = a
End Sub

Private Sub UserForm_Initialize()
Dim d As Object, i&, a
tablo = Sheets("Feuil1").Range("B2", Range("C" & Rows.Count).End(xlUp)(2))
Set d = CreateObject("Scripting.Dictionary")
For i = 1 To UBound(tablo)
  If tablo(i, 2) <> "" Then d(tablo(i, 2)) = ""
Next
If d.Count Then
  a = d.keys
  tri a, 0, UBound(a)
  ComboBox1.List = a
End If
End Sub

Sub tri(a, gauc, droi)       ' Quick sort
Dim ref, g, d, temp
ref = a((gauc + droi) \ 2)
g = gauc: d = droi
Do
    Do While a(g) < ref: g = g + 1: Loop
    Do While ref < a(d): d = d - 1: Loop
    If g <= d Then
      temp = a(g): a(g) = a(d): a(d) = temp
      g = g + 1: d = d - 1
    End If
Loop While g <= d
If g < droi Then Call tri(a, g, droi)
If gauc < d Then Call tri(a, gauc, d)
End Sub
Fichier (2).

A+
 

Pièces jointes

  • ILINO COMBOBOX LISTE(2).xlsm
    29.8 KB · Affichages: 27

Ilino

XLDnaute Barbatruc
Re : [-RESOLUTO-]ComboBox qui depend d'un autre ComboBox

Re maitre
GRAZIE
Mais ton dernier fichier affiche un message d'erreur d’exécution 32809
Erreur d"finie par l'application ou par l'objet
A++
 
Dernière édition:

Dranreb

XLDnaute Barbatruc
Re : [-RESOLUTO-]ComboBox qui depend d'un autre ComboBox

Bonjour.

Ce serait quand même considérablement plus simple, plus facile, plus concis et plus sûr (coté programmation "applicative" évidemment, inutile d'étudier la programmation de service) avec un objet ComboBoxCasc ou ComboBoxLiés.

Edit: Bonne année Job75.
 
Dernière édition:

job75

XLDnaute Barbatruc
Re : [-RESOLUTO-]ComboBox qui depend d'un autre ComboBox

Re, salut Bernard et bonne année,

Sur le fichier du post #1 chez moi il n'y a aucun bug.

Quelle ligne de code beugue Ilino ? Joins ton fichier réel avec le code que tu y as mis.

A+
 

Discussions similaires

Statistiques des forums

Discussions
311 729
Messages
2 081 966
Membres
101 852
dernier inscrit
dthi16088