Problème dictionnary avec combobox en cascade
Bonjour à tous,
Mon premier POST sur ce FOFO. Voila quelques temps que je me mets à faire des fichiers excel en VBA et en grande partie grace à vous qui partagez des infos géniales pour avancer !!!!
Seulement voilà depuis plusieurs heures impossible de résoudre mon problème qui doit être pourtant très idiot...
Je m'en remet à votre aide pour me dépatouiller.
Le fichier servira à faire une gestion de stock. il est constitué :
- D'un tableau de bord pour que l'opérateur créer de nouveaux produits, ajoute des entrées et sorties de stocks, etc
- D'une base de données sur laquelle nous trouverons la liste des produits créés et surtout la mise à jour du stock
- D'une base E et d'une base S
Mon problème :
Lorsque l'opérateur ajoute une entrée ou sortie, je souhaite qu'il sélectionne un numéro de produit (colonne F de BDD) dans la première combobox, et que la seconde indique toutes les références possibles de ce produit.
Sauf que dans mon cas, le premier tri étant un numéro de produit, la seconde combobox ne s'alimente pas !
Si je remplace mes numéros de produit par du texte... ça fonctionne !
J'ai essayé d'autres méthodes mais sans succès car j'aimerai trier, éliminer les doublons. Dictionnary semble être la meilleure solution...
Merci beaucoup pour votre aide. D'avance une grand merci.
Salut terminos87 et
à tester
Private Sub ComboBox1_Change()
Set f = Sheets("BDD")
Set Mondico = CreateObject("Scripting.Dictionary")
For Each C In Range(f.[F8], f.[F65536].End(xlUp))
If C = CStr(Me.ComboBox1) Then Mondico(C.Offset(, 1).Text) = C.Offset(, 1).Text
Next C
Me.ComboBox2.List = Mondico.Keys '.items
Me.ComboBox2.ListIndex = -1
'Me.Liste_Type_Carte.ListIndex = -1
'Me.Liste_Ref_Carte.ListIndex = -1
End Sub@++
PUNAISE !!!!! IT WORKS !
La fonction Cstr converti en texte si je comprends bien ?
Woalala... Un grand merci ! J'ai passé 6 heures sur ce truc !!!!!
oui c'est ca, elle converti une valeur en Text
donc le code initial devrait fonctionner aussi
Private Sub ComboBox1_Change()
Set f = Sheets("BDD")
Set Mondico = CreateObject("Scripting.Dictionary")
For Each C In Range(f.[F8], f.[F65536].End(xlUp))
If C = CStr(Me.ComboBox1) Then Mondico(C.Offset(, 1).Text) = C.Offset(, 1).Text
Next C
Me.ComboBox2.List = Mondico.items
Me.ComboBox2.ListIndex = -1
'Me.Liste_Type_Carte.ListIndex = -1
'Me.Liste_Ref_Carte.ListIndex = -1
End SubBonne journée
Super, merci !
Si je veux ordonner par ordre croissant, il existe des fonctions du dico à utiliser ou je dois faire une macro de tri spécifique ?
J'ai fais ça, ça fonctionne sur ma première combo, et sur la deuxième pas très bien.
la deuxième j'ai 100,21,22,56... Que fais le 100 ici ^^
Private Sub UserForm_Initialize()
Dim i As Integer
Dim f
TextBox1 = Sheets("ITEM").Range("A" & 25)
TextBox1.Value = Format(TextBox1.Value, "0#")
valeur = CDbl(TextBox1.Value)
TextBox2 = Sheets("ITEM").Range("A" & 26)
TextBox2.Value = Format(TextBox2.Value, "0#")
valeur = CDbl(TextBox2.Value)
TextBox3 = Sheets("ITEM").Range("A" & 27)
TextBox3.Value = Format(TextBox3.Value, "0#")
valeur = CDbl(TextBox3.Value)
Set f = Sheets("BDD")
Set MonDico = CreateObject("Scripting.Dictionary")
For Each C In Range(f.[F8], f.[F65536].End(xlUp))
MonDico(C.Value) = C.Value
Next C
Me.ComboBox1.List = MonDico.items
'appel fonction tri
temp = MonDico.Keys
Call Tri(temp, LBound(temp), UBound(temp))
Me.ComboBox1.List = temp
End SubPrivate Sub ComboBox1_Change()
Set f = Sheets("BDD")
Set MonDico2 = CreateObject("Scripting.Dictionary")
For Each C In Range(f.[F8], f.[F65536].End(xlUp))
If C = CStr(Me.ComboBox1) Then MonDico2(C.Offset(, 1).Text) = C.Offset(, 1).Text
Next C
Me.ComboBox2.List = MonDico2.Keys '.items
Me.ComboBox2.ListIndex = -1
'Me.Liste_Type_Carte.ListIndex = -1
'Me.Liste_Ref_Carte.ListIndex = -1
'appel fonction tri
temp2 = MonDico2.Keys
Call Tri(temp2, LBound(temp2), UBound(temp2))
Me.ComboBox2.List = temp2
End SubSub 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 SubRe,
essaie comme ca
Private Sub ComboBox1_Change()
Set f = Sheets("BDD")
Set MonDico2 = CreateObject("Scripting.Dictionary")
For Each C In Range(f.[F8], f.[F65536].End(xlUp))
If C = CStr(Me.ComboBox1) Then MonDico2(CSng(C.Offset(, 1).Text)) = C.Offset(, 1).Text
Next C
Me.ComboBox2.List = MonDico2.Items
Me.ComboBox2.ListIndex = -1
'Me.Liste_Type_Carte.ListIndex = -1
'Me.Liste_Ref_Carte.ListIndex = -1
'appel fonction tri
temp2 = MonDico2.Keys
Call Tri(temp2, LBound(temp2), UBound(temp2))
Me.ComboBox2.List = temp2
End Sub@++
Au top m3ellem1... C'est hyper gentil !
Vraiment un grand merci pour ta réactivité et ton aide précieuse !
A bientôt
mais avec plaisir, c'est toi qui a presque tout fait!
En plus j'apprécie beaucoup beaucoup les personnes comme toi, qui font un effort pour atteindre leurs buts et quand ca coinçe il viennent chercher une solution ou un petit coup de main
bonne continuation et à la prochaine!