Change colour to show which button was last clicked

fawnlemur

New Member
Joined
Dec 9, 2018
Messages
29
Hi

I've researched and I can' find what I want the code do:

I want a code that will change a button's colour when it's clicked, but when another button is clicked it would change that button's colour and reset the previous ones.

I'm not too sure if there is a way to do it without linking to a specific button as I've got too many button.

I have tried to do it but came out with nothing, so I don't have any decent example code.

Thank you very much for your help
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
There are probably better ways of doing this, but using the macro recorder I got code like this . . .
Code:
    ActiveSheet.Shapes.Range(Array("Rectangle 1")).Select
    With Selection.ShapeRange.Fill
        .Visible = msoTrue
        .ForeColor.RGB = RGB(255, 255, 0)
        .Transparency = 0
        .Solid
    End With
    
    ActiveSheet.Shapes.Range(Array("Rectangle 2", "Rectangle 3")).Select
    With Selection.ShapeRange.Fill
        .Visible = msoTrue
        .ForeColor.RGB = RGB(255, 0, 255)
        .Transparency = 0
        .Solid
    End With

If by button you mean a shape such as a rectangle, and you assign the macro including this code to rectangle 1, then it turns rectangle 1 yellow, and rectangles 2 and 3 purple.
Play with the RGB values to get different colours.
 
Upvote 0
If your using Activex command buttons.

This script loops through all the Active sheet activex command buttons
I wrote this script but I do not know how to exclude the command button that activated the script.

Code:
Private Sub CommandButton1_Click()
'Modified  1/16/2019  9:12:21 AM  EST
For Each OLEobj In ActiveSheet.OLEObjects
    If OLEobj.progID = "Forms.CommandButton.1" Then
        With OLEobj.Object
            .Font.Size = 16
            .BackColor = vbRed
            .ForeColor = vbBlue
        End With
    End If
Next OLEobj
End Sub
 
Upvote 0
If your using Activex command buttons.

This script loops through all the Active sheet activex command buttons
I wrote this script but I do not know how to exclude the command button that activated the script.

Code:
Private Sub CommandButton1_Click()
'Modified  1/16/2019  9:12:21 AM  EST
For Each OLEobj In ActiveSheet.OLEObjects
    If OLEobj.progID = "Forms.CommandButton.1" Then
        With OLEobj.Object
            .Font.Size = 16
            .BackColor = vbRed
            .ForeColor = vbBlue
        End With
    End If
Next OLEobj
End Sub

Would this work on normal buttons as I'm not using ActiveX buttons

Thank you
 
Upvote 0
Well My script really does not do what you wanted. It was just a part of the code needed. Maybe someone else here will have a complete answer.
 
Upvote 0
I have discovered Form Control Command Buttons cannot have colors. They all must be default Gray.
 
Upvote 0
Is there any way just to make it look like it was clicked so it looks different from the other buttons


Thanks
 
Upvote 0
I'm curious what all these buttons do.

Normally a user should not need numerous buttons on a sheet. There are a lot of better ways to do things then to have numerous buttons doing things.

Did you know you can just click or double click or right click on a cell to have a script run.
And having to put scripts into numerous buttons can be a real task.
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,224
Members
452,620
Latest member
dsubash

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