Backcolor Textbox avec condition

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

keepcool183

XLDnaute Occasionnel
Bonjour à tous les forumeurs 🙂

Je cherche à créer un code pour que le fond du textbox se colorie selon si sa valeur appartient à une intervalle.

Exemple :

Si txtbox.value > 25 alors backcolor rouge.

j'ai fais ce code mais il ne marche pas ou plutot ne prend pas en considération les différentes intervalles.

Code:
 If TextBox2.Value < Sheets("data").Range("f2").Value Then
TextBox2.BackColor = RGB(255, 0, 0)
ElseIf TextBox2.Value < Sheets("data").Range("f3").Value Then
TextBox2.BackColor = RGB(100, 50, 0)
ElseIf TextBox2.Value < Sheets("data").Range("f4").Value Then
TextBox2.BackColor = RGB(50, 100, 20)
ElseIf TextBox2.Value < Sheets("data").Range("f5").Value Then
TextBox2.BackColor = RGB(200, 100, 250)
ElseIf TextBox2.Value < Sheets("data").Range("f6").Value Then
TextBox2.BackColor = RGB(200, 10, 25)
End If

Merci à ceux qui me liront 😉
 
Re : Backcolor Textbox avec condition

Bonjour Pascal,

Merci pour l'astuce, je comprend maintenant pourquoi je n'y arrive pas.

en fait c'est parce que les valeurs sont des pourcentages.

J'ai essayé d'arranger la chose avec Cdbl mais rien à faire.

je joins donc un fichier exemple 🙂
 

Pièces jointes

Re : Backcolor Textbox avec condition

re

essaies ainsi
Code:
Private Sub UserForm_Initialize()
'*********** Mise en place Textbox en % ****************
TextBox1.Value = Format(Sheets("marché").Range("G8").Value, "0.0%")
TextBox2.Value = Format(Sheets("marché").Range("H8").Value, "0.0%")
TextBox3.Value = Format(Sheets("marché").Range("L8").Value, "0.0%")
TextBox4.Value = Format(Sheets("marché").Range("M8").Value, "0.0%")
TextBox5.Value = Format(Sheets("marché").Range("N8").Value, "0.0%")
'******
toto = CDbl(Left(TextBox2.Value, Len(TextBox2.Value) - 1)) / 100
If toto < Sheets("data").Range("A2").Value Then
 TextBox2.BackColor = RGB(255, 0, 0)
ElseIf toto < Sheets("data").Range("A3").Value Then
TextBox2.BackColor = RGB(100, 50, 0)
ElseIf toto < Sheets("data").Range("A4").Value Then
TextBox2.BackColor = RGB(50, 100, 20)
ElseIf toto < Sheets("data").Range("A5").Value Then
TextBox2.BackColor = RGB(200, 100, 250)
ElseIf toto < Sheets("data").Range("A6").Value Then
TextBox2.BackColor = RGB(200, 10, 25)
End If
End Sub
 
- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD

Discussions similaires

Réponses
2
Affichages
462
Réponses
68
Affichages
8 K
Retour