VBA - Colour change buttons with timer

Jasesair

Active Member
Joined
Apr 8, 2015
Messages
282
Office Version
  1. 2016
I am wanting to utilise colour changing buttons to essentially highlight a button when pressed. After 1 or 2 seconds, I'd like the button to revert back to the original colour. Is this possible? This is what I have so far...

VBA Code:
Sub SetColor(v)
  Dim cell As String
  cell = ActiveCell.Address
  ActiveSheet.Shapes.SelectAll
  Selection.ShapeRange.Fill.ForeColor.RGB = RGB(175, 171, 171)
  ActiveSheet.Shapes(v).Fill.ForeColor.RGB = RGB(0, 204, 153)
  Range(cell).Activate
End Sub

VBA Code:
Sub Offerings1()

    Application.ScreenUpdating = False
    Call SetColor(Application.Caller)

    Worksheets("Offerings").Range("c7,e7,g7,i7").Interior.ColorIndex = xlNone

End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
You can set up a separate procedure to reset the button's color, and then use the OnTime method of the Application object to call that procedure after one second has elapsed. So, for example, place the following code in a regular module, and then try running it.

VBA Code:
Option Explicit

'these two variables must be declared as Public variables
Public button As Shape
Public buttonColor As Long

Sub Offerings1()

    Application.ScreenUpdating = False
    
    Call SetColor(Application.Caller)

    Worksheets("Offerings").Range("c7,e7,g7,i7").Interior.ColorIndex = xlNone

End Sub

Sub SetColor(v)

  Dim cell As String
  cell = ActiveCell.Address
 
  Set button = ActiveSheet.Shapes(v)
 
  buttonColor = button.Fill.ForeColor.RGB
 
  ActiveSheet.Shapes.SelectAll
  Selection.ShapeRange.Fill.ForeColor.RGB = RGB(175, 171, 171)
 
  button.Fill.ForeColor.RGB = RGB(0, 204, 153)
 
  Application.OnTime Now() + TimeValue("00:00:01"), "'ResetButton button, buttonColor'" 'calls function after one second has elapsed (change as desired)
 
  Range(cell).Activate
 
End Sub

Public Sub ResetButton(ByVal button As Shape, ByVal buttonColor As Long)

    button.Fill.ForeColor.RGB = buttonColor
    
End Sub

Hope this helps!
 
Upvote 0
Actually, I would do it this way instead. First, insert a new regular module (Visual Basic Editor >> Insert >> Module), and then place the following code in the code module...

VBA Code:
Option Explicit

'these two variables must be declared as Public variables
Public button As Shape
Public buttonColor As Long

Public Sub ResetButton(ByVal button As Shape, ByVal buttonColor As Long)

    button.Fill.ForeColor.RGB = buttonColor
    
End Sub

Then, right-click the sheet tab for the sheet containing your button and shapes, and then place the following code in the code module...

VBA Code:
Option Explicit

Private Sub Offerings1()

    Call SetColor(Application.Caller)

    Worksheets("Offerings").Range("c7,e7,g7,i7").Interior.ColorIndex = xlNone

End Sub

Private Sub SetColor(v)

  Dim cell As String
  cell = ActiveCell.Address
 
  Set button = Me.Shapes(v)
 
  buttonColor = button.Fill.ForeColor.RGB
 
  Dim shapesArray() As String
  ReDim shapesArray(0 To Me.Shapes.Count - 1)
 
  Dim shp As Shape
  Dim cnt As Long
  For Each shp In Me.Shapes
    If shp.Name <> button.Name Then
        shapesArray(cnt) = shp.Name
        cnt = cnt + 1
    End If
  Next shp
 
  Me.Shapes.Range(shapesArray).Fill.ForeColor.RGB = RGB(175, 171, 171)
 
  button.Fill.ForeColor.RGB = RGB(0, 204, 153)
 
  Application.OnTime Now() + TimeValue("00:00:01"), "'ResetButton button, buttonColor'" 'calls function after one second has elapsed (change as desired)
    
End Sub

Lastly, let's say that your workbook is called Book1.xlsm, and that the codename (not the worksheet name) for the worksheet containing your button and shapes is called Sheet1, assign the macro to your button by right-clicking on the button, selecting Assign Macro, entering the macro name as 'Book1.xlsm'!Sheet1.Offerings1, and clicking on OK.

Hope this helps!
 
Upvote 0
Hi Domenic, many thanks for your help. I won't pretend to fully understand what's happening in your most recent suggestions, but I'm wondering why this is a better option than your first suggestion?
 
Upvote 0
First, all of the code related to the worksheet itself is contained within the code module for the sheet. Secondly, the procedures have all been delcared as Private, so that the only way to access them is through the button. And, lastly, it avoids the use of SelectAll and Selection, which usually is not as efficient and can cause problems when the sheet is not the active sheet. Although, in this case, I don't think it would really be a problem. Nevertheless, I still like to avoid doing any selecting.
 
Upvote 0
Thanks for that explanation. Sounds like the way to go. I managed to get this to work but now when applying this learning in a different workbook, I'm failing miserably. I keep getting a Visual Basic 400 error and can't for the life of me see where I'm going wrong.

I have the below public sub in a module:
VBA Code:
Option Explicit

Public button As Shape
Public buttonColor As Long

Public Sub ResetButton(ByVal button As Shape, ByVal buttonColor As Long)

    button.Fill.ForeColor.RGB = buttonColor
    
End Sub

And, I have the below in the sheet module.

VBA Code:
Private Sub Reset_Units()

    Call SetColor(Application.Caller)

End Sub
Private Sub Reset_Workbook()

    Call SetColor(Application.Caller)

End Sub

Private Sub SetColor(v)

  Dim cell As String
  cell = ActiveCell.Address
 
  Set button = Me.Shapes(v)
 
  buttonColor = button.Fill.ForeColor.RGB
 
  Dim shapesArray() As String
  ReDim shapesArray(0 To Me.Shapes.Count - 1)
 
  Dim shp As Shape
  Dim cnt As Long
  For Each shp In Me.Shapes
    If shp.Name <> button.Name Then
        shapesArray(cnt) = shp.Name
        cnt = cnt + 1
    End If
  Next shp
 
  Me.Shapes.Range(shapesArray).Fill.ForeColor.RGB = RGB(175, 171, 171)
 
  button.Fill.ForeColor.RGB = RGB(0, 204, 153)
 
  Application.OnTime Now() + TimeValue("00:00:02"), "'ResetButton button, buttonColor'"

End Sub

I have assigned the macro as you suggested.
 
Upvote 0
It's just one that says Visual Basic For Applications with a X warning symbol and 400.
 
Upvote 0
A few things to try...

  1. Try stepping through the code one line at a time using the F8 key to see where the error occurs.
  2. Try moving the code from the regular module to another regular module.
  3. Try the code in a new workbook.
  4. Try allowing access to the VBA project module (File > Options > Trust Center > Trust Center Settings > Macro Settings > select/check Trust access to the VBA project object module).
Does this help?
 
Upvote 0
I hope I'm using F8 properly... it appears there is a mismatch run-time error 13 on line: buttonColor = button.Fill.ForeColor.RGB
 
Upvote 0

Forum statistics

Threads
1,215,417
Messages
6,124,787
Members
449,188
Latest member
Hoffk036

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