ComboBox, sélection automatique quand 1 seul choix possible

Résolu
ti_mouton Messages postés 153 Statut Membre -  
ti_mouton Messages postés 153 Statut Membre -
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

1 réponse

  1. gbinforme Messages postés 14930 Date d'inscription   Statut Contributeur Dernière intervention   4 744
     
    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
    1. ti_mouton Messages postés 153 Statut Membre
       
      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
      1. gbinforme Messages postés 14930 Date d'inscription   Statut Contributeur Dernière intervention   4 744 > ti_mouton Messages postés 153 Statut Membre
         
        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
    2. ti_mouton Messages postés 153 Statut Membre
       
      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
      1. gbinforme Messages postés 14930 Date d'inscription   Statut Contributeur Dernière intervention   4 744 > ti_mouton Messages postés 153 Statut Membre
         
        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
    3. ti_mouton Messages postés 153 Statut Membre
       
      ça fonctionne ! merci beaucoup :)
      0