VBA to change colour of button when pressed

Jasesair

Active Member
Joined
Apr 8, 2015
Messages
282
Office Version
  1. 2016
I have 10 buttons (like a keypad), each with assigned macro to do certain things. I'd like a line of code that I can add to each button's macro that changes the colour of the button when pressed. Idea being, when the button is pressed, it becomes a different colour and the remainder of the code then happens. The button would then need to revert back to its original colour when another of the 10 buttons is pressed.

Hoping this is an easy one!!
 
How would I change your code above to only change specific shapes, not all.

Try this.
In this line of the macro put the name of the specific shapes.

Case "shape1", "shape2", "shape3", "etc"

VBA Code:
Sub SetColor(v)
  Dim cell As String
  Dim shp As Shape
  cell = ActiveCell.Address
  For Each shp In ActiveSheet.Shapes
    Select Case shp.Name
      Case "shape1", "shape2", "shape3", "etc"    'Shape name, is case sensitive
        shp.Fill.ForeColor.RGB = RGB(175, 171, 171)
    End Select
  Next
  ActiveSheet.Shapes(v).Fill.ForeColor.RGB = RGB(224, 255, 124)
  Range(cell).Activate
End Sub
 
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Try this.
In this line of the macro put the name of the specific shapes.

Case "shape1", "shape2", "shape3", "etc"

VBA Code:
Sub SetColor(v)
  Dim cell As String
  Dim shp As Shape
  cell = ActiveCell.Address
  For Each shp In ActiveSheet.Shapes
    Select Case shp.Name
      Case "shape1", "shape2", "shape3", "etc"    'Shape name, is case sensitive
        shp.Fill.ForeColor.RGB = RGB(175, 171, 171)
    End Select
  Next
  ActiveSheet.Shapes(v).Fill.ForeColor.RGB = RGB(224, 255, 124)
  Range(cell).Activate
End Sub
Worked perfectly! Thanks so much!!!!
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,947
Members
449,095
Latest member
nmaske

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