C/Combobox : Afficher titres tableau et mettre en rouge le texte "NC"
Bonsoir tut le monde,
je souhaite afficher dans un combobox le contenu d'un tableau (colonne=6) en affichant les titres Et dans la colonne F, si le contenu de la cellule = NC, je souhaite que NC soit en rouge.
J'ai fait les codes suivants mais cela ne fonctionne pas.
Merci de votre aide
Juju
Private Sub UserForm_Initialize()
'*********************************************************
'remplir la combobox
'le combobox pour faire le choix de fiche
' de la consultation, de la modification, de la levée NC
'*********************************************************
With ComboBox1
Me.ComboBox1.ColumnCount = 6
Me.ComboBox1.ColumnWidths = "80 pt;200 pt;170 pt;49.95 pt;120 pt;60 pt"
Me.ComboBox1.ColumnHeads = True
End With
Call dproF("Parametres")
Dim ligne As Integer
Dim ligne_param As Long
Dim derlig_param As Long
derlig_param = ThisWorkbook.Sheets("Parametres").Range("AA" & Rows.Count).End(xlUp).Row
ThisWorkbook.Sheets("Parametres").Range("AA2:AA" & derlig_param).ClearContents
Dim derlig_BDD As Long
derlig_BDD = ThisWorkbook.Sheets("BDD").Range("A" & Rows.Count).End(xlUp).Row
ligne = 2
ligne_param = 2
If levee_NC = True Then
With UserForm10
.ComboBox1.Clear
For ligne = 2 To derlig_BDD
If ThisWorkbook.Sheets("BDD").Range("F" & ligne) = "NC" And ThisWorkbook.Sheets("BDD").Range("K" & ligne) = "" Then
With UserForm10.ComboBox1
.AddItem
.List(ligne_param - 2, 0) = ThisWorkbook.Sheets("BDD").Range("A" & ligne)
.List(ligne_param - 2, 1) = ThisWorkbook.Sheets("BDD").Range("B" & ligne)
.List(ligne_param - 2, 2) = ThisWorkbook.Sheets("BDD").Range("C" & ligne)
.List(ligne_param - 2, 3) = ThisWorkbook.Sheets("BDD").Range("D" & ligne)
.List(ligne_param - 2, 4) = ThisWorkbook.Sheets("BDD").Range("E" & ligne)
.List(ligne_param - 2, 5) = ThisWorkbook.Sheets("BDD").Range("F" & ligne)
End With
UserForm10.ComboBox1.ListIndex = -1 'initialisation au premier
.ComboBox1.AddItem _
' ThisWorkbook.Sheets("BDD").Range("A" & ligne) & " " & _
' ThisWorkbook.Sheets("BDD").Range("B" & ligne) & " " & _
' ThisWorkbook.Sheets("BDD").Range("C" & ligne) & " " & _
' ThisWorkbook.Sheets("BDD").Range("D" & ligne)
ThisWorkbook.Sheets("Parametres").Range("AA" & ligne_param) = ligne
ligne_param = ligne_param + 1
End If
Next
End With
Else
With UserForm10
.ComboBox1.Clear
For ligne = 2 To derlig_BDD
With UserForm10.ComboBox1
.AddItem
.List(ligne - 2, 0) = ThisWorkbook.Sheets("BDD").Range("A" & ligne)
.List(ligne - 2, 1) = ThisWorkbook.Sheets("BDD").Range("B" & ligne)
.List(ligne - 2, 2) = ThisWorkbook.Sheets("BDD").Range("C" & ligne)
.List(ligne - 2, 3) = ThisWorkbook.Sheets("BDD").Range("D" & ligne)
.List(ligne - 2, 4) = ThisWorkbook.Sheets("BDD").Range("E" & ligne)
.List(ligne - 2, 5) = ThisWorkbook.Sheets("BDD").Range("F" & ligne)
End With
UserForm10.ComboBox1.ListIndex = -1 'initialisation au premier
.ComboBox1.AddItem _
'ThisWorkbook.Sheets("BDD").Range("A" & ligne) & " " & _
'ThisWorkbook.Sheets("BDD").Range("B" & ligne) & " " & _
'ThisWorkbook.Sheets("BDD").Range("C" & ligne) & " " & _
'ThisWorkbook.Sheets("BDD").Range("D" & ligne)
Next
End With
End If
Call proF("Parametres")
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("BDD")
If Target.Column = 6 Then ' Colonne F
For Each cell In Target
If cell.Value = "NC" Then
' Changez la couleur du texte dans le ComboBox1
Me.ComboBox1.ForeColor = RGB(255, 0, 0) ' Rouge
Else
' Rétablissez la couleur du texte par défaut
Me.ComboBox1.ForeColor = RGB(0, 0, 0) ' Noir
End If
Next cell
End If
End Sub- Messages
- 4'199
- Excel
- 2021 FR 64 bits
- Inscrit
- 13/06/2016
- Emploi
- bénévole associations Goutte d'Or
Bonjour,
Pour afficher les titres, vous avez 2 solutions :
1- utiliser la propriété RowSource sous l'expresse réserve que votre tableau ne sera pas modifié dans le code de votre UserForm.
2- inclure les titres dans le chargement de votre Combobox avec une instruction dans votre code interdisant leur sélection.
Pour la couleur, elle ne peut être que globale pour la Combobox et non spécifique à un de ses éléments.
Bonjour thev,
merci pour la réponse. je vais faire et voir si cela fonctionne.
Bon dimanche
Juju