sheet with many command buttons, one must be coloured

robertvdb

Active Member
Joined
Jan 10, 2021
Messages
327
Office Version
  1. 2016
Platform
  1. Windows
I have a Sheet with multiple command buttons, 12 of them indicate the months of the year. When I press either of those 12, it must become orange. ALL the others must stay in grey.

Is there an easy way to do this ? Thanks.
 

Attachments

  • buttons.png
    buttons.png
    43.2 KB · Views: 12

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Simplest but not most compact:

Use one sub to set all buttons back to gray, another sub to update the one that was pressed. Replace the button names here with your actual button names. Adjust colors as desired.

VBA Code:
Public Sub SetGray()

   button1.BackColor = RGB(66, 66, 66)
   Button2.BackColor = RGB(66, 66, 66)
   Button3.BackColor = RGB(66, 66, 66)
   Button4.BackColor = RGB(66, 66, 66)
   Button5.BackColor = RGB(66, 66, 66)
   Button6.BackColor = RGB(66, 66, 66)
   Button7.BackColor = RGB(66, 66, 66)
   Button8.BackColor = RGB(66, 66, 66)
   Button9.BackColor = RGB(66, 66, 66)
   Button10.BackColor = RGB(66, 66, 66)
   Button11.BackColor = RGB(66, 66, 66)
   Button12.BackColor = RGB(66, 66, 66)

End Sub

' Repeat the following sub for each button
Public Sub Button1_Click()

   SetGray
   button1.BackColor = RGB(198, 89, 17)

End Sub
 
Upvote 0
Another way:
VBA Code:
Sub UpdateButtonsColors()
    Dim oBtn As OLEObject, oAcc As IAccessible
    For Each oBtn In ActiveSheet.OLEObjects
        With oBtn
            If TypeOf .Object Is CommandButton Then
                If Not IsError(Evaluate("=MONTH(1&""" & .Object.Caption & """)")) Then
                    Set oAcc = .Object
                    If oAcc.accState(0&) = &H100004 Then
                        .Visible = False
                        .Object.BackColor = RGB(198, 89, 17)
                        .Visible = True
                    Else
                        .Object.BackColor = &H8000000F
                    End If
                End If
            End If
        End With
    Next
End Sub

Then call UpdateButtonsColors in each button click event.
 
Upvote 1
Solution
Thanks Jaafar, your solution works fine. Also thanks to Jeff.
 
Upvote 0

Forum statistics

Threads
1,215,161
Messages
6,123,378
Members
449,097
Latest member
Jabe

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