Ceci est une page optimisée pour les mobiles. Cliquez sur ce texte pour afficher la vraie page.

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
 

TooFatBoy

XLDnaute Barbatruc
Re : Simplification de code..

Bonjour,

Essaye voir un truc dans ce genre :

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

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 ...
 

Evelynetfrancois

XLDnaute Impliqué
Re : Simplification de code..

re JORDAN , Marcel32

merci pour votre rapidité
jordan pourrais tu stp me developper un peu plus ta sujetion
j essaye dans tous les sens ... sans resultat LOL

(je ne suis pas dans l urgence)

merci pour tout !!!!!

E et F
 

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.


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:

Evelynetfrancois

XLDnaute Impliqué
Re : Simplification de code..

RE Marcel
Avec le temps ..... J’ai fini par comprendre !!!
En tout cas M E R C I pour tout
J’ai revu et corrigé plein de bout de code avec tout ça !!

Bonne journée, au plaisir de te lire à nouveau.
E et F
 

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:

Evelynetfrancois

XLDnaute Impliqué
Re : Simplification de code..

Bonjour MARCEL
Dans mon cas , ça ne me pose aucun problèmes
Dans mon programme les cas de figure possible que tu as relevé ,n arrive jamais
donc ................super !!!!
bonne aprem à toi
cordialement
E et F
 
Les cookies sont requis pour utiliser ce site. Vous devez les accepter pour continuer à utiliser le site. En savoir plus…