Changement 3 couleurs bouton commande VBA
Bonjour à tous,
Je cherche à faire un programme qui permet de changer la couleur des mes boutons commandes (en activeX). Après quelque recherche sur le net (ici également, je ne sais plus où j'ai trouvé le code), j'ai ceci :
Dim B as Byte
Private Sub CommandButton11_Click()
B = B + 1
If B > 3 Then B = 1
If B = 1 Then CommandButton11.BackColor = RGB(255, 128, 128)
If B = 2 Then CommandButton11.BackColor = RGB(128, 255, 128)
If B = 3 Then CommandButton11.BackColor = RGB(128, 128, 255)
End Sub
Cette formule fonctionne bien, aucun problème .... ou presque (sinon je ne serais pas la
Mon programme sert de check list pour mes équipes, equipe matin = rouge, apres midi = vert, nuit = bleu. Le problème : l'ordinateur est parfois éteind (minimum une fois par semaine). Le problème est donc le suivant :
En rallumant le PC, la couleur s’initialise sur B=1, or je voudrais que le cycle ne soit pas interrompu et reprenne donc à la dernière couleur enregistré (j'ai une macro qui enregistre, il faut juste que j'assure la reprise ...)
Une solution ?
Bonjour
Une solution peut être en attribuant une valeur à B en fonction de la couleur du bouton trouvée à l'ouverture du fichier
Private Sub Workbook_Open()
Select Case Sheets(1).CommandButton11.BackColor
Case Is = RGB(128, 128, 255): B = 3
Case Is = RGB(128, 255, 128): B = 2
Case Is = RGB(255, 128, 128): B = 1
End Select
End SubA placer dans Thisworkbook
Le mieux serait de voir le fichier...
Si ok, oubliez pas de cloturer le fil en cliquant sur le v vert à coté du bouton Editer lors de votre réponse
Cordialement
edit :
Essayez ceci
Bonjour,
Merci pour cette réponse rapide, malheureusement cette solution ne marche pas ... (pourtant sur le papier c'est tout à fait logique et ce que je cherche à faire, j'ai peut être mal écrit ?)
Mon fichier est trop lourd pour etre mis (2.5Mo)
Encore merci pour votre aide
Option Explicit
Dim A As Byte
Dim B As Byte
Dim C As Byte
Dim D As Byte
Dim E As Byte
Dim F As Byte
Dim G As Byte
Dim H As Byte
Dim i As Byte
Dim j As Byte
Public N As Byte
Dim PauseTime, Start, Finish, TotalTime
Sub testnuit()
If Sheets("Maintenance hebdomadaire").Range("N24").Value = 0 Then
MsgBox "Maintenance hebdomadaire à faire"
End If
End Sub
Sub testmatin()
If Sheets("Maintenance hebdomadaire").Range("N5").Value = 0 Then
MsgBox "Maintenance hebdomadaire à faire"
End If
End Sub
Sub testapresmidi()
If Sheets("Maintenance hebdomadaire").Range("N14").Value = 0 Then
MsgBox "Maintenance hebdomadaire à faire"
End If
End Sub
Sub macro3()
If N = 1 Then
Sheets("compteur").Range("E16").End(xlDown).Offset(1, 0).Value = Time
End If
If N = 2 Then
Sheets("compteur").Range("F16").End(xlDown).Offset(1, 0).Value = Time
End If
If N = 3 Then
Sheets("compteur").Range("G16").End(xlDown).Offset(1, 0).Value = Time
End If
End Sub
Sub Macro1()
If CommandButton1.BackColor = RGB(255, 128, 128) = True Then
Sheets("compteur").Range("A1").Value = Sheets("compteur").Range("A1").Value + 1
Else
If CommandButton1.BackColor = RGB(128, 255, 128) = True Then
Sheets("compteur").Range("A2").Value = Sheets("compteur").Range("A2").Value + 1
Else
If CommandButton1.BackColor = RGB(128, 128, 255) = True Then
Sheets("compteur").Range("A3").Value = Sheets("compteur").Range("A3").Value + 1
End If
End If
End If
End Sub
Sub Macro2()
ActiveWorkbook.Save
End Sub
Private Sub CommandButton1_Click()
N = N + 1
If N > 3 Then N = 1
If N = 1 Then CommandButton1.BackColor = RGB(255, 128, 128)
If N = 2 Then CommandButton1.BackColor = RGB(128, 255, 128)
If N = 3 Then CommandButton1.BackColor = RGB(128, 128, 255)
Macro1 'incrementation compteur suivi
macro3 'inscrire la date de réalisation
couleur 'remise à 0 des couleurs
If CommandButton1.BackColor = RGB(255, 128, 128) Then
testmatin 'popup maintenance hebdo à faire matin
End If
If CommandButton1.BackColor = RGB(128, 255, 128) Then
testapresmidi 'popup maintenance hebdo a faire apres midi
End If
If CommandButton1.BackColor = RGB(128, 128, 255) Then
testnuit 'popup maintenance hebdo nuit
End If
Macro2 'sauvegarde
End Sub
Private Sub CommandButton2_Click()
Sheets("compteur").Range("A1").Value = 0
Sheets("compteur").Range("A2").Value = 0
Sheets("compteur").Range("A3").Value = 0
End Sub
Private Sub CommandButton3_Click()
C = C + 1
If C > 3 Then C = 1
If C = 1 Then CommandButton3.BackColor = RGB(255, 128, 128)
If C = 2 Then CommandButton3.BackColor = RGB(128, 255, 128)
If C = 3 Then CommandButton3.BackColor = RGB(128, 128, 255)
End Sub
Private Sub CommandButton4_Click()
D = D + 1
If D > 3 Then D = 1
If D = 1 Then CommandButton4.BackColor = RGB(255, 128, 128)
If D = 2 Then CommandButton4.BackColor = RGB(128, 255, 128)
If D = 3 Then CommandButton4.BackColor = RGB(128, 128, 255)
End Sub
Private Sub CommandButton5_Click()
E = E + 1
If E > 3 Then E = 1
If E = 1 Then CommandButton5.BackColor = RGB(255, 128, 128)
If E = 2 Then CommandButton5.BackColor = RGB(128, 255, 128)
If E = 3 Then CommandButton5.BackColor = RGB(128, 128, 255)
End Sub
Private Sub CommandButton6_Click()
F = F + 1
If F > 3 Then F = 1
If F = 1 Then CommandButton6.BackColor = RGB(255, 128, 128)
If F = 2 Then CommandButton6.BackColor = RGB(128, 255, 128)
If F = 3 Then CommandButton6.BackColor = RGB(128, 128, 255)
End Sub
Private Sub CommandButton7_Click()
G = G + 1
If G > 3 Then G = 1
If G = 1 Then CommandButton7.BackColor = RGB(255, 128, 128)
If G = 2 Then CommandButton7.BackColor = RGB(128, 255, 128)
If G = 3 Then CommandButton7.BackColor = RGB(128, 128, 255)
End Sub
Private Sub CommandButton8_Click()
H = H + 1
If H > 3 Then H = 1
If H = 1 Then CommandButton8.BackColor = RGB(255, 128, 128)
If H = 2 Then CommandButton8.BackColor = RGB(128, 255, 128)
If H = 3 Then CommandButton8.BackColor = RGB(128, 128, 255)
End Sub
Private Sub CommandButton9_Click()
i = i + 1
If i > 3 Then i = 1
If i = 1 Then CommandButton9.BackColor = RGB(255, 128, 128)
If i = 2 Then CommandButton9.BackColor = RGB(128, 255, 128)
If i = 3 Then CommandButton9.BackColor = RGB(128, 128, 255)
End Sub
Private Sub CommandButton10_Click()
A = A + 1
If A > 3 Then A = 1
If A = 1 Then CommandButton10.BackColor = RGB(255, 128, 128)
If A = 2 Then CommandButton10.BackColor = RGB(128, 255, 128)
If A = 3 Then CommandButton10.BackColor = RGB(128, 128, 255)
End Sub
Private Sub CommandButton11_Click()
B = B + 1
If B > 3 Then B = 1
If B = 1 Then CommandButton11.BackColor = RGB(255, 128, 128)
If B = 2 Then CommandButton11.BackColor = RGB(128, 255, 128)
If B = 3 Then CommandButton11.BackColor = RGB(128, 128, 255)
End Sub
Private Sub CommandButton12_Click()
j = j + 1
If j > 3 Then j = 1
If j = 1 Then CommandButton12.BackColor = RGB(255, 128, 128)
If j = 2 Then CommandButton12.BackColor = RGB(128, 255, 128)
If j = 3 Then CommandButton12.BackColor = RGB(128, 128, 255)
End Sub
Re
Ces macros se trouvent dans un module ou dans une seule feuille ?
Si le fichier est trop lourd, peut être essayer d'éliminer des données ?
wow, jai suprimer les liens hypertextes sur photo, je pensais pas que ca prennait autant de place... m'enfin, si joint le fichier
Bonjour
Bien vu votre fichier mais il y a des codes partout. Pouvez-vous réexpliquer votre souci par rapport au fichier envoyé et me donner un exemple que je puisse comprendre
Cordialement
Bonjour
je pense a un truc comme ca
Private Sub CommandButton2_Click()
Coul = Range("O2").Value
N = Coul + 1
If N > 3 Then N = 1
If N = 1 Then CommandButton2.BackColor = RGB(255, 128, 128)
If N = 2 Then CommandButton2.BackColor = RGB(128, 255, 128)
If N = 3 Then CommandButton2.BackColor = RGB(128, 128, 255)
Range("O2").Value = N
End Sub
Private Sub CommandButton3_Click()
Coul = Range("O3").Value
N = Coul + 1
If N > 3 Then N = 1
If N = 1 Then CommandButton3.BackColor = RGB(255, 128, 128)
If N = 2 Then CommandButton3.BackColor = RGB(128, 255, 128)
If N = 3 Then CommandButton3.BackColor = RGB(128, 128, 255)
Range("O3").Value = N
End Suba voir si ca te va
A+
Maurice
Merci pour les réponses rapides !
le problème est uniquement sur la feuille 1 (Maintenance).
Le problème est que lorsque j'ouvre mon fichier, la variable qui défini les couleurs s'initialise à 1 alors que je veux reprendre le cycle de couleur là où je l'avais laissé avant de fermé.
J'ai tester rapidement la solution d'archer sur un bouton, ca a l'air de fonctionner ! Je test sur les autres boutons et valide que sa fonctionne bien partout comme je veux au bureau dès lundi !
Bon w-e à vous
Bonjour
Dans la feuille Maintenance, vous supprimez les déclaration de variables Dim a as byte, Dim B as byte, etc... et vous les mettez dans le module 1 comme ceci :
Public A as byte
Public B as byte
Public C as byte
....Bien entendu cela suppose que vous sauver chaque fois votre fichier en le fermant.
Certains de vos modules ne contiennent pas de codes. Vous pouvez les supprimer. Un seul avec les codes c'est plus facile
Maintenant en regardant votre fichier, il me semble que l'on aurait pu simplifier mais bon cela fonctionne
Si souci dites moi
Cordialement
C'est tout bon ! Merci Dan et Archer !
Je pense aussi qu'il y a plus simple et plsu propre pour faire ce code mais comme tu dis, ca fonctionne donc c'est le principale (et puis c'est pas trop mal pour mon premier code vba de ma vie ...)