• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

How to assign a macro to a command button.

sunnyp

Member
I want a macro that can change the color of the command button when it click one time(green) and click again It will turn to red color.
 
You can't do that with a command button. You can assign a macro to a regular shape, and then change the color of that shape. See here:

http://chandoo.org/forums/topic/macro-to-change-shape-colour
 
Hi, sunnyp!


If I haven't misunderstood what you asked for, I disagree with Luke M, you can do it with a simple one-sentence macro. Give a look at this file:

http://dl.dropbox.com/u/60558749/How%20to%20assign%20a%20macro%20to%20a%20command%20button.%20%28for%20sunnyp%20at%20chandoo.org%29.xlsm


Just advise if any issue.


Regards!


@Luke M


Hi!


Regarding your CASFFML trick, here's a copy of the code:

-----

[pre]
Code:
Option Explicit

Private Sub cmdLittleCommandButton1_Click()
cmdLittleCommandButton1.BackColor = RGB(Int(Rnd() * 256), Int(Rnd() * 256), Int(Rnd() * 256))
End Sub

Private Sub cmdLittleCommandButton2_Click()
cmdLittleCommandButton2.BackColor = IIf(cmdLittleCommandButton2.BackColor = vbRed, vbGreen, vbRed)
End Sub
[/pre]
-----


Two examples: first, random coloring; second, red/green cycling as requested.


Regards!
 
SirJB7,

Ah, looks like I should have been clearer. You can't change the color of a Forms, command button, but you can change an ActiveX command button (which has many more VB abilities than it's cousin).
 
@Luke M

Hi!

Yes, remember a couple of weeks ago? http://chandoo.org/forums/topic/macro-to-change-shape-colour

Regards!
 
How come this code does not work in my workbook. never mind thank you for the code it is working now. thanks.
 
Back
Top