command button to change colour

jammoca

Banned
Joined
Nov 6, 2002
Messages
1,100
I have a userform that contains 4 command buttons representing the 4 quarters of a football match.

The user will need to press the"1st Qtr" button when the game begins and then the "2nd Qtr" button when the 2nd quarter begins etc etc.

Each of the buttons is currently grey, but I would like the most recently pressed button to become green and stay green until the next button is pressed, which in turn would become green and the previous button return to being grey.

Is this possible ?
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Crude but siomple way,


Right click one of your commandButtons and choose 'View Code'
Then paste in the below
Code:
Private Sub CommandButton1_Click()
CommandButton1.BackColor = 49152
CommandButton2.BackColor = -2147483633
CommandButton3.BackColor = -2147483633
CommandButton4.BackColor = -2147483633
End Sub
Private Sub CommandButton2_Click()
CommandButton1.BackColor = -2147483633
CommandButton2.BackColor = 49152
CommandButton3.BackColor = -2147483633
CommandButton4.BackColor = -2147483633
End Sub
Private Sub CommandButton3_Click()
CommandButton1.BackColor = -2147483633
CommandButton2.BackColor = -2147483633
CommandButton3.BackColor = 49152
CommandButton4.BackColor = -2147483633
End Sub
Private Sub CommandButton4_Click()
CommandButton1.BackColor = -2147483633
CommandButton2.BackColor = -2147483633
CommandButton3.BackColor = -2147483633
CommandButton4.BackColor = 49152
End Sub

Cheers
GB
 
Upvote 0
That works great, but the green was a little too dark.

Is there code for a bright yellow ?

I didn't recognise the code you supplied. It doesn't look like the RGB or the Hex codes I would normally play with.

Where can I access the codes you refer to ?
 
Upvote 0
Try this green

CommandButton1.BackColor = &H80FF80

It might be a bit easier on the eyes.

To change the color of a button manually, right click your Button and choose view Code.

In the properties menu (usually at the bottom left of the VBE) click BackColor and you will see all the available colors. This is where I get the numbers from.

Cheers
GB
 
Upvote 0
You can also use the RGB function to set a control's color
Code:
CommandButton1.BackColor = RGB(0,255,255)
 
Upvote 0

Forum statistics

Threads
1,213,535
Messages
6,114,198
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top