Simplification de code..

Evelynetfrancois

XLDnaute Impliqué
BONJOUR à tous !!
Que de lignes que de lignes ....................

En fonction de la valeur de la TextBox4 je colore certain label.....
Toutes ces lignes fonctionnent parfaitement, mais je pense qu il y a beaucoup plus "propre, voir moins lourd !!!" en VBA

si vous avez une solution de rêve .... Je suis preneur
Merci pour tout
A bientôt vous lire
E et F

(Les valeurs différentes de TB4 ne sont pas une suite logique)


If TextBox4.Value = "1" Then
Label4.BackColor = &HC000&
End If
If TextBox4.Value = "2" Then
Label5.BackColor = &HC000&
End If
If TextBox4.Value = "3" Then
Label6.BackColor = &HC000&
End If
If TextBox4.Value = "4" Then
Label7.BackColor = &HC000&
End If
If TextBox4.Value = "5" Then
Label8.BackColor = &HC000&
End If
If TextBox4.Value = "12" Then
Label4.BackColor = &HC000&
Label5.BackColor = &HC000&
End If
If TextBox4.Value = "13" Then
Label4.BackColor = &HC000&
Label6.BackColor = &HC000&
End If
If TextBox4.Value = "14" Then
Label4.BackColor = &HC000&
Label7.BackColor = &HC000&
End If
If TextBox4.Value = "15" Then
Label4.BackColor = &HC000&
Label8.BackColor = &HC000&
End If
If TextBox4.Value = "23" Then
Label5.BackColor = &HC000&
Label6.BackColor = &HC000&
End If
If TextBox4.Value = "24" Then
Label5.BackColor = &HC000&
Label7.BackColor = &HC000&
End If
If TextBox4.Value = "25" Then
Label5.BackColor = &HC000&
Label8.BackColor = &HC000&
End If
If TextBox4.Value = "34" Then
Label6.BackColor = &HC000&
Label7.BackColor = &HC000&
End If
If TextBox4.Value = "35" Then
Label6.BackColor = &HC000&
Label8.BackColor = &HC000&
End If
If TextBox4.Value = "45" Then
Label7.BackColor = &HC000&
Label8.BackColor = &HC000&
End If
If TextBox4.Value = "123" Then
Label4.BackColor = &HC000&
Label5.BackColor = &HC000&
Label6.BackColor = &HC000&
End If
If TextBox4.Value = "124" Then
Label4.BackColor = &HC000&
Label5.BackColor = &HC000&
Label7.BackColor = &HC000&
End If
If TextBox4.Value = "125" Then
Label4.BackColor = &HC000&
Label5.BackColor = &HC000&
Label8.BackColor = &HC000&
End If
If TextBox4.Value = "134" Then
Label4.BackColor = &HC000&
Label6.BackColor = &HC000&
Label7.BackColor = &HC000&
End If
If TextBox4.Value = "135" Then
Label4.BackColor = &HC000&
Label6.BackColor = &HC000&
Label8.BackColor = &HC000&
End If
If TextBox4.Value = "145" Then
Label4.BackColor = &HC000&
Label7.BackColor = &HC000&
Label8.BackColor = &HC000&
End If
If TextBox4.Value = "234" Then
Label5.BackColor = &HC000&
Label6.BackColor = &HC000&
Label7.BackColor = &HC000&
End If
If TextBox4.Value = "235" Then
Label5.BackColor = &HC000&
Label6.BackColor = &HC000&
Label8.BackColor = &HC000&
End If
If TextBox4.Value = "245" Then
Label5.BackColor = &HC000&
Label7.BackColor = &HC000&
Label8.BackColor = &HC000&
End If
If TextBox4.Value = "345" Then
Label6.BackColor = &HC000&
Label7.BackColor = &HC000&
Label8.BackColor = &HC000&
End If
If TextBox4.Value = "1234" Then
Label4.BackColor = &HC000&
Label5.BackColor = &HC000&
Label6.BackColor = &HC000&
Label7.BackColor = &HC000&
End If
If TextBox4.Value = "1235" Then
Label4.BackColor = &HC000&
Label5.BackColor = &HC000&
Label6.BackColor = &HC000&
Label8.BackColor = &HC000&
End If
If TextBox4.Value = "1245" Then
Label4.BackColor = &HC000&
Label5.BackColor = &HC000&
Label7.BackColor = &HC000&
Label8.BackColor = &HC000&
End If
If TextBox4.Value = "1345" Then
Label4.BackColor = &HC000&
Label6.BackColor = &HC000&
Label7.BackColor = &HC000&
Label8.BackColor = &HC000&
End If
If TextBox4.Value = "2345" Then
Label5.BackColor = &HC000&
Label6.BackColor = &HC000&
Label7.BackColor = &HC000&
Label8.BackColor = &HC000&
End If
If TextBox4.Value = "12345" Then
For I = 4 To 8
Me.Controls("Label" & I).BackColor = &HC000&
Next I
End If
 

JORDAN

XLDnaute Impliqué
Re : Simplification de code..

Bonjour Evelynetfrancois

peut être comme ceci :
Choix, variable contenant la valeur à rechercher
Code:
select case Choix
case 1
Label4.BackColor = &HC000&

case 2
Label5.BackColor = &HC000&

case 3
Label6.BackColor = &HC000&

case ...
 

TooFatBoy

XLDnaute Barbatruc
Re : Simplification de code..

Si le code que je t'ai donné ne fonctionne pas, merci de m'expliquer ce que ça crée comme problème, afin que j'essaye de voir où est mon erreur. :eek:


Le code proposé par Jordan est très similaire au tien, et c'est effectivement la première idée qui vient : remplacer les IF...ENDIF par un SELECT...CASE.
 
Dernière édition:

Evelynetfrancois

XLDnaute Impliqué
Re : Simplification de code..

RE Marcel32
Le code que tu m’as donné marche parfaitement bien ...oups !!
mais comme je ne comprends pas son fonctionnement je ne l’avais même pas essayé , pensant tout simplement que tu n’avais pas compris mon problème ……………….je me confonds en excuses vraiment désolé ..............


For I = 1 To 5
If InStr(TextBox4.Value, I) <> 0 Then Me.Controls("Label" & (I + 3)).BackColor = &HC000&
Next I

Pourquoi
For I = 4 To 8
If InStr(TextBox4.Value, I) <> 0 Then Me.Controls("Label" & I).BackColor = &HC000&
Next I
Ne marche pas ?

Vraiment merci pour tout !!!
E et F
 

TooFatBoy

XLDnaute Barbatruc
Re : Simplification de code..

Bonsoir,

No problemo. ;)


J'avais également pensé à ce que tu as essayé de faire.
Mais dans ce cas, il faut légèrement modifier le début du code :
For I = 4 To 8
If InStr(TextBox4.Value, I-3) <> 0 Then Me.Controls("Label" & I).BackColor = &HC000&
Next I


Si tu veux des explications sur le fonctionnement de la ligne de code utilisée dans la boucle FOR...NEXT, n'hésite pas à me demander. ;)
 
Dernière édition:

TooFatBoy

XLDnaute Barbatruc
Re : Simplification de code..

A ton service. ;)


Bon dimanche.


[edit]
A T T E N T I O N
Je viens de penser à un truc embêtant !! :(
Si TextBox4.Value est égale à "21", on a le même résultat que si elle est égale à "12", ou pire "1 29 7671" ou même "Bozo est 1 clown comme il n'y en a pas 2 !" par exemple.

Autrement dit ça fonctionne parfaitement pour les cas que tu avais programmés au départ, mais ça réagit de la même façon dans d'autres cas... :(
Il suffit qu'il y ait un 1 pour que Label4.BackColor prenne la couleur &HC000& et il en va de même pour les quatre autres valeurs (de 2 à 5).

A toi de voir si c'est gênant pour l'utilisation de ton programme.
[/edit]
 
Dernière édition:

Statistiques des forums

Discussions
312 248
Messages
2 086 594
Membres
103 250
dernier inscrit
keks974