ComboBox

Résolu/Fermé
ti_mouton Messages postés 143 Date d'inscription vendredi 29 mai 2015 Statut Membre Dernière intervention 5 septembre 2020 - Modifié par pijaku le 18/06/2015 à 09:55
pijaku Messages postés 12263 Date d'inscription jeudi 15 mai 2008 Statut Modérateur Dernière intervention 4 janvier 2024 - 18 juin 2015 à 10:33
Bonjour,

J'ai un userform avec plusieurs Combobox, les ComboBox 3 et 4 sont liées et la combobox 5 qui elle est indépendante des autres. Je voudrais rajouter un code qui fasse en sorte que lorsqu'il n'y a qu'un seul choix possible pour cette combobox il se selectionne automatiquement? J'ai tenté quelques codes avec ce que j'ai pu trouver sur ce forum mais sans succès...

Private Sub Userform_Initialize()
  Set f = Sheets("BDD")
  Set mondico = CreateObject("Scripting.Dictionary")
  For Each C In f.Range("C3:C" & f.[A65000].End(xlUp).Row)
    mondico(C.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
  Set mondico = CreateObject("Scripting.Dictionary")
  For Each C In f.Range("C3:C" & f.[A65000].End(xlUp).Row)
    If C = Me.ComboBox3 Then mondico(C.Offset(, 1).Value) = ""
  Next C
  temp = mondico.keys
  Call Tri(temp, LBound(temp), UBound(temp))
  Me.ComboBox4.List = temp
    If Me.ComboBox4.ListCount = 1 Then Me.ComboBox4.ListIndex = 0
End Sub
Private Sub ComboBox5_Click()
  Sheets("BDD").Select
  ComboBox5.List = Range("E3:E" & f.[A65000].End(xlUp).Row).Value
  temp = mondico.keys
  Call Tri(temp, LBound(temp), UBound(temp))
  Me.ComboBox5.List = temp
  If Me.ComboBox5.ListCount = 1 Then Me.ComboBox5.ListIndex = 0
End Sub
Sub Tri(a, gauc, droi) ' Quick sort
  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


Merci pour votre aide,

2 réponses

pijaku Messages postés 12263 Date d'inscription jeudi 15 mai 2008 Statut Modérateur Dernière intervention 4 janvier 2024 2 743
18 juin 2015 à 09:34
Bonjour,

Et si, avant de poser une nouvelle question, tu allais répondre à tes sujets en cours :
https://forums.commentcamarche.net/forum/affich-32119334-erreur-70-permission-refusee
0
ti_mouton Messages postés 143 Date d'inscription vendredi 29 mai 2015 Statut Membre Dernière intervention 5 septembre 2020
18 juin 2015 à 09:43
Excuses moi, je pensais t'avoir répondu mais visiblement j'ai du faire une fausse manip ou oublié de valider ma réponse...
0
pijaku Messages postés 12263 Date d'inscription jeudi 15 mai 2008 Statut Modérateur Dernière intervention 4 janvier 2024 2 743
18 juin 2015 à 09:55
Pas de souci.
C'est fait.

Pour te répondre donc aujourd'hui.
Quel est l'événement qui, jusqu'à maintenant, remplit ta ComboBox5?
L'événement ComboBox5_Click.
Or, aujourd'hui, tu souhaiterais qu'elle soit "pré-remplie" si elle n'a qu'un seul élément.
Il te faut trouver donc l'événement déclencheur de cela.
Par exemple, tu peut ajouter le remplissage de ta combobox5 dans l'événement Initialize de l'UserForm.
La globalité de ton code donné plus haut deviendrait donc :
Private Sub Userform_Initialize()
  Set f = Sheets("BDD")
  Set mondico = CreateObject("Scripting.Dictionary")
  For Each C In f.Range("C3:C" & f.[A65000].End(xlUp).Row)
    mondico(C.Value) = ""
  Next C
  temp = mondico.keys
  Call Tri(temp, LBound(temp), UBound(temp))
  Me.ComboBox3.List = temp
  Set mondico = CreateObject("Scripting.Dictionary")
  For Each C In f.Range("E3:E" & f.[A65000].End(xlUp).Row)
    mondico(C.Value) = ""
  Next C
  temp = mondico.keys
  Call Tri(temp, LBound(temp), UBound(temp))
  Me.ComboBox5.List = temp
  If Me.ComboBox5.ListCount = 1 Then Me.ComboBox5.ListIndex = 0
End Sub
Private Sub ComboBox3_click()
  Me.ComboBox4.Clear
  Set mondico = CreateObject("Scripting.Dictionary")
  For Each C In f.Range("C3:C" & f.[A65000].End(xlUp).Row)
    If C = Me.ComboBox3 Then mondico(C.Offset(, 1).Value) = ""
  Next C
  temp = mondico.keys
  Call Tri(temp, LBound(temp), UBound(temp))
  Me.ComboBox4.List = temp
    If Me.ComboBox4.ListCount = 1 Then Me.ComboBox4.ListIndex = 0
End Sub
Private Sub ComboBox5_Click()

End Sub
Sub Tri(a, gauc, droi) ' Quick sort
  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

A tester...
0
ti_mouton Messages postés 143 Date d'inscription vendredi 29 mai 2015 Statut Membre Dernière intervention 5 septembre 2020
18 juin 2015 à 10:32
C'est parfait ! merci pour tes explications
0
pijaku Messages postés 12263 Date d'inscription jeudi 15 mai 2008 Statut Modérateur Dernière intervention 4 janvier 2024 2 743 > ti_mouton Messages postés 143 Date d'inscription vendredi 29 mai 2015 Statut Membre Dernière intervention 5 septembre 2020
18 juin 2015 à 10:33
Mais de rien.
A++
0