ComboBox, sélection automatique quand 1 seul choix possible

Résolu/Fermé
ti_mouton Messages postés 143 Date d'inscription vendredi 29 mai 2015 Statut Membre Dernière intervention 5 septembre 2020 - 11 juin 2015 à 21:04
ti_mouton Messages postés 143 Date d'inscription vendredi 29 mai 2015 Statut Membre Dernière intervention 5 septembre 2020 - 12 juin 2015 à 10:07
Bonjour,

Voici ma question, j'ai 2 ComboBox en cascade, dans la ComboBox1 je sélectionne mon "projet", suite à cela la ComboBox2 me propose la liste des activités qui sont liées au projet que je viens de choisir. Existe il un code qui permet de faire en sorte que lorsqu'il n'y a qu'un seul choix possible dans la Combobox2 il est sélectionné d'office, on a pas besoin de cliqué dessus pour le sélectionné.

Merci pour votre aide
A voir également:

1 réponse

gbinforme Messages postés 14946 Date d'inscription lundi 18 octobre 2004 Statut Contributeur Dernière intervention 24 juin 2020 4 686
Modifié par gbinforme le 11/06/2015 à 21:12
Bonjour,

Il suffit de vérifier lorsque tu le documentes que .ListItems.Count est à un et d'appeler la fonction de sélection ( .ListIndex=0).
Toujours zen
La perfection est atteinte, non pas lorsqu'il n'y a plus rien à ajouter, mais lorsqu'il n'y a plus rien à retirer. Antoine de Saint-Exupéry
0
ti_mouton Messages postés 143 Date d'inscription vendredi 29 mai 2015 Statut Membre Dernière intervention 5 septembre 2020
Modifié par ti_mouton le 11/06/2015 à 21:26
Bonsoir gbinforme,

Pourrais tu être plus précis stp... je suis dsl je suis très débutante en VBA.

Voici le code que l'on ma fourni sur ce forum, si ça peut t'aider à m'éclairer. J'ai en fait 5 ComBobox.

Private Sub UserForm_Initialize()
Set f = Sheets("BDD")
Set mondico = CreateObject("Scripting.Dictionary")
For Each C In f.Range("A3:A" & f.[A65000].End(xlUp).Row)
mondico(C.Value) = ""
Next C
temp = mondico.keys
Call Tri(temp, LBound(temp), UBound(temp))
Me.ComboBox1.List = temp
End Sub
Private Sub ComboBox1_click()
Me.ComboBox2.Clear
Me.ComboBox3.Clear
Me.ComboBox4.Clear
Me.ComboBox5.Clear
Set mondico = CreateObject("Scripting.Dictionary")
For Each C In f.Range("A3:A" & f.[A65000].End(xlUp).Row)
If C = Me.ComboBox1 Then mondico(C.Offset(, 1).Value) = ""
Next C
temp = mondico.keys
Call Tri(temp, LBound(temp), UBound(temp))
Me.ComboBox2.List = temp
End Sub
Private Sub ComboBox2_click()
Me.ComboBox3.Clear
Me.ComboBox4.Clear
Me.ComboBox5.Clear
Set mondico = CreateObject("Scripting.Dictionary")
For Each C In f.Range("A3:A" & f.[A65000].End(xlUp).Row)
If C = Me.ComboBox1 And C.Offset(, 1) = Me.ComboBox2 Then mondico(C.Offset(, 2).Value) = ""
Next C
temp = mondico.keys
Call Tri(temp, LBound(temp), UBound(temp))
Me.ComboBox3.List = temp
End Sub
Private Sub ComboBox3_click()
Me.ComboBox4.Clear
Me.ComboBox5.Clear
Set mondico = CreateObject("Scripting.Dictionary")
For Each C In f.Range("A3:A" & f.[A65000].End(xlUp).Row)
If C = Me.ComboBox1 And C.Offset(, 1) = Me.ComboBox2 And C.Offset(, 2).Value = Me.ComboBox3 Then mondico(C.Offset(, 3).Value) = ""
Next C
temp = mondico.keys
Call Tri(temp, LBound(temp), UBound(temp))
Me.ComboBox4.List = temp
End Sub
Private Sub ComboBox4_click()
Me.ComboBox5.Clear
Set mondico = CreateObject("Scripting.Dictionary")
For Each C In f.Range("A3:A" & f.[A65000].End(xlUp).Row)
If C = Me.ComboBox1 And C.Offset(, 1) = Me.ComboBox2 And C.Offset(, 2).Value = Me.ComboBox3 And C.Offset(, 3).Value = Me.ComboBox4 Then mondico(C.Offset(, 4).Value) = ""
Next C
temp = mondico.keys
Call Tri(temp, LBound(temp), UBound(temp))
Me.ComboBox5.List = temp
End Sub


Merci
0
gbinforme Messages postés 14946 Date d'inscription lundi 18 octobre 2004 Statut Contributeur Dernière intervention 24 juin 2020 4 686 > ti_mouton Messages postés 143 Date d'inscription vendredi 29 mai 2015 Statut Membre Dernière intervention 5 septembre 2020
11 juin 2015 à 22:22
Après ton instruction (sous lignée), l'instruction en gras déclenche "ComboBox2_click" ce qui correspond à la fonctionnalité que tu voulais :
 Me.ComboBox2.List = temp
if Me.ComboBox2.ListItems.Count = 1 then Me.ComboBox2.ListIndex=0
0
ti_mouton Messages postés 143 Date d'inscription vendredi 29 mai 2015 Statut Membre Dernière intervention 5 septembre 2020
12 juin 2015 à 09:33
Je viens d'intégrer ce code mais j'ai le message d'erreur suivant : erreur de compilation : membre de méthode ou de données introuvable.
Avez vous une idée ?
Merci
0
gbinforme Messages postés 14946 Date d'inscription lundi 18 octobre 2004 Statut Contributeur Dernière intervention 24 juin 2020 4 686 > ti_mouton Messages postés 143 Date d'inscription vendredi 29 mai 2015 Statut Membre Dernière intervention 5 septembre 2020
12 juin 2015 à 09:57
Au temps pour moi, j'ai mélangé avec le code listview :
If Me.ComboBox2.ListCount = 1 Then Me.ComboBox2.ListIndex = 0

Cela devrait fonctionner.
0
ti_mouton Messages postés 143 Date d'inscription vendredi 29 mai 2015 Statut Membre Dernière intervention 5 septembre 2020
12 juin 2015 à 10:07
ça fonctionne ! merci beaucoup :)
0