VBA : Problème d'initialisation de combobox

syl0038

XLDnaute Junior
Bonjour,
Quelqu'un pourrait t'il m'expliquer comment initialiser ma combobox 4 dans l'userform production 1.
Cela fonctionne à l'ouverture de l'userform production1 mais ensuite quand je change la valeur dans le combobox4, rien ne se passe.

J'ai essayé des codes en faisant des recherches mais sans grand succès.

Je vous remercie par avance de votre aide!
Syl0038

Le code pour la macro est 38520
 

Pièces jointes

  • syl0038 (2).xlsm
    166.6 KB · Affichages: 88

Gelinotte

XLDnaute Accro
Re : VBA : Problème d'initialisation de combobox

Bonjour,

Il faut réinitialier les items avant traitement.

Code:
Private Sub combobox4_reset()
    Production1.ComboBox1.Visible = False
    Production1.ComboBox2.Visible = False
    Production1.ComboBox3.Visible = False
    Production1.TextBox1.Visible = False
    Production1.TextBox2.Visible = False
    Production1.TextBox3.Visible = False
    Production1.CheckBox1.Visible = False
    Production1.Label30.Visible = False
    Production1.Label5.Visible = False
    Production1.Label4.Visible = False
    Production1.Label6.Visible = False
    Production1.Label7.Visible = False
    Production1.Label27.Visible = False
    Production1.Label27.Visible = False
End Sub



Private Sub ComboBox4_Change()
Dim MonDico As Object
Set MonDico = CreateObject("Scripting.Dictionary")
combobox4_reset
....... la suite du code
 

pierrejean

XLDnaute Barbatruc
Re : VBA : Problème d'initialisation de combobox

Bonjour syl0038

Et que diantre devrait-il se passer ???
Tu ne mets rien dans mondico et par ailleurs ces lignes sont inutiles !!!

Code:
    Production1.ComboBox4.List = MonDico.items
    Production1.ComboBox4.ListIndex = -1
    Production1.ComboBox4.List = Sheets("Data_base").Range("n2:n7").Value

Par ailleurs , a l'intention de tous ceux qui passeront par la
Evitez de nous faire etudier des codes non fonctionnels sans nous preciser à quoi ils sont destinés (nous ne sommes pas extralucides !!!!)

Ah !!!
Bravo Gelinotte
De ce qui precede ,je conclus que tu es EXTRALUCIDES (et moi NON)
 

Papou-net

XLDnaute Barbatruc
Re : VBA : Problème d'initialisation de combobox

Bonsoir sil0038, les participants,

Comme j'ai planché sur le sujet, je joins ma solution dans laquelle j'ai restructuré le code et supprimé les lignes inutiles.

Je partage entièrement la remarque de PierreJean car j'ai rencontré les mêmes problèmes de compréhension. Ce qui fait que je n'ai pas renseigné les deux dernières options de la liste dans la condition Select Case...End Select car je n'ai pas retrouvé la correspondance dans le code d'origine.

Par ailleurs, le fait d'effacer la ComboBox4 après le choix conduisait à effacer les contrôles : j'ai donc ajouté un test de condition en première ligne.

Voici le code :

Code:
Private Sub ComboBox4_Change()
If ComboBox4.ListIndex = -1 Then Exit Sub

    Production1.ComboBox1.Visible = False
    Production1.ComboBox2.Visible = False
    Production1.ComboBox3.Visible = False
    Production1.TextBox1.Visible = False
    Production1.TextBox2.Visible = False
    Production1.TextBox3.Visible = False
    Production1.CheckBox1.Visible = False
    Production1.Label30.Visible = False
    Production1.Label5.Visible = False
    Production1.Label4.Visible = False
    Production1.Label6.Visible = False
    Production1.Label7.Visible = False
    Production1.Label27.Visible = False
'-----------------------------------------------------------------------------------
Select Case ComboBox4.ListIndex
  Case 0 ' Moulage/assemblage
    Production1.ComboBox1.Visible = True
    Production1.ComboBox2.Visible = True
    Production1.ComboBox3.Visible = True
    Production1.TextBox1.Visible = True
    Production1.TextBox2.Visible = True
    Production1.TextBox3.Visible = True
    Production1.CheckBox1.Visible = True
    Production1.Label30.Visible = True
    Production1.Label5.Visible = True
    Production1.Label4.Visible = True
    Production1.Label6.Visible = True
    Production1.Label7.Visible = True
    Production1.Label27.Visible = True
      Case 1 ' Sortare
    Production1.ComboBox3.Visible = True
    Production1.TextBox3.Visible = True
    Production1.Label4.Visible = True
    Production1.Label27.Visible = True
  Case 2 ' Ambalare
    Production1.ComboBox3.Visible = True
    Production1.TextBox3.Visible = True
    Production1.Label4.Visible = True
    Production1.Label27.Visible = True
  Case 3 ' Control 100%
    Production1.ComboBox3.Visible = True
    Production1.TextBox3.Visible = True
    Production1.Label4.Visible = True
    Production1.Label27.Visible = True
  Case 4 ' Formare
  Case 5 ' Sedinta
End Select
Production1.ComboBox4.ListIndex = -1
End Sub
Cordialement.
 
Dernière édition:

Papou-net

XLDnaute Barbatruc
Re : VBA : Problème d'initialisation de combobox

RE :

Et pour faire encore plus court :

Code:
Private Sub ComboBox4_Change()
If ComboBox4.ListIndex = -1 Then Exit Sub
Select Case ComboBox4.ListIndex
  Case 0 ' Moulage/assemblage
    ctl = Array("1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1")
  Case 1 ' Sortare
    ctl = Array("0", "0", "1", "0", "0", "1", "0", "0", "0", "1", "0", "0", "1")
  Case 2, 3 ' Ambalare, Control 100%
    ctl = Array("0", "0", "1", "0", "0", "1", "0", "0", "0", "1", "0", "0", "1")
  Case 4 ' Formare *
    ctl = Array("0", "0", "1", "0", "0", "1", "0", "0", "0", "1", "0", "0", "1")
  Case 5 ' Sedinta *
    ctl = Array("0", "0", "1", "0", "0", "1", "0", "0", "0", "1", "0", "0", "1")
End Select
Production1.ComboBox1.Visible = IIf(ctl(0) = "1", True, False)
Production1.ComboBox2.Visible = IIf(ctl(1) = "1", True, False)
Production1.ComboBox3.Visible = IIf(ctl(2) = "1", True, False)
Production1.TextBox1.Visible = IIf(ctl(3) = "1", True, False)
Production1.TextBox2.Visible = IIf(ctl(4) = "1", True, False)
Production1.TextBox3.Visible = IIf(ctl(5) = "1", True, False)
Production1.CheckBox1.Visible = IIf(ctl(6) = "1", True, False)
Production1.Label30.Visible = IIf(ctl(7) = "1", True, False)
Production1.Label5.Visible = IIf(ctl(8) = "1", True, False)
Production1.Label4.Visible = IIf(ctl(9) = "1", True, False)
Production1.Label6.Visible = IIf(ctl(10) = "1", True, False)
Production1.Label7.Visible = IIf(ctl(11) = "1", True, False)
Production1.Label27.Visible = IIf(ctl(12) = "1", True, False)
Production1.ComboBox4.ListIndex = -1
End Sub
Si les Case 4 et 5 (*) sont identiques à d'autres on peut les reporter sur ces lignes en séparant par une virgule comme dans Case 2, 3.

Cordialement.
 

Discussions similaires

Réponses
6
Affichages
952

Statistiques des forums

Discussions
311 720
Messages
2 081 904
Membres
101 834
dernier inscrit
Jeremy06510