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.
 

Kenneth Hobson

Well-known Member
Joined
Feb 6, 2007
Messages
3,092
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
 

Foo_Man_Chu

Board Regular
Joined
Jul 22, 2010
Messages
79
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.
 

Forum statistics

Threads
1,081,748
Messages
5,361,041
Members
400,610
Latest member
ebey

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top