Trying to change the color of a command button

Foo_Man_Chu

Board Regular
Joined
Jul 22, 2010
Messages
79
Hi all,
I'm trying to change the backcolor of a command button with the on click event of the button. Seeing that I will have several buttons to change the color or, I made a separate sub to change the button backcolor with that I can just call from the on click event of the button in question. Here's my code:

Code:
Private Sub A3btn_Click()
    ChangeButtonColor A3btn
End Sub


Private Sub ChangeButtonColor(strBtnName As CommandButton)
    If strBtnName.BackColor = vbWhite Then 'white
        strBtnName.BackColor = vbGreen 'green
    ElseIf strBtnName.BackColor = vbGreen Then 'green
        strBtnName.BackColor = vbRed 'red
    ElseIf strBtnName.BackColor = vbRed Then 'red
        strBtnName.BackColor = vbGreen 'green
    End If
End Sub

it should be pretty self explanatory and not that complex. However it's not behaving as I expect it to. Here's what's happening:

The button's backcolor is white and is clicked for the first time. It turns green.(Expected result) If I wait a second or two and click it again, it turns red. (Expected result). However, if I click it rapidly it takes 2 or 3 clicks to get it to turn red and 2 or 3 more clicks to turn it back to green(Not expected, it should only take one click to change back and forth between the colors).



I've looked for solutions for quite a while but no dice. I thought maybe for some reason the sub that changes the color was getting run more than once but I checked for that and I don't think that's the case. I thought that it might be another sub that was interfering so I deleted everything except these two subs and it still happens. Oh, I'm running Excel 2013 on Windows 10. And the button in question is an ActiveX command button.

Any help would be much appreciated. Thanks in advance.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Maybe adding a doubleclick event will get what you want?

Code:
Private Sub A3btn_Click()
  ChangeButtonColor A3btn
End Sub

Private Sub A3btn_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
  ChangeButtonColor A3btn
End Sub

Private Sub ChangeButtonColor(aBtn As CommandButton)
  With aBtn
    Select Case True
      Case .BackColor = vbWhite
        .BackColor = vbGreen
      Case .BackColor = vbGreen
        .BackColor = vbRed
      Case .BackColor = vbRed
        .BackColor = vbGreen
      Case Else
        .BackColor = vbWhite  '16777215
    End Select
  End With
End Su
 
Upvote 0
Thanks for the suggestion Kenneth. I tried out the doubleclick and it's hard to tell if it works because the clicking behavior is so erratic to begin with.
 
Upvote 0

Forum statistics

Threads
1,214,520
Messages
6,120,013
Members
448,935
Latest member
ijat

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