Change VBA Button Color?

Talk about your favorite PC games, Steam and building awesome custom rigs!

Moderator: Moderators

Post Reply
Positron
Posts: 53
Joined: Mon Mar 31, 2008 11:10 pm

Change VBA Button Color?

Post by Positron »

I want to be able to click a button (not an autoshape) and have the button itself (not the text) change colors. I have searched everywhere and cannot seem to find the answer.
jeffslot
Portablizer Extraordinaire
Posts: 853
Joined: Wed Jun 01, 2005 6:26 am
Location: PA
Contact:

Post by jeffslot »

Come on now, you couldn't have looked everywhere....

Code: Select all

commandbutton1.BackColor = vbGreen
I want to be able to click a button (not an autoshape) and have the button itself (not the text) change colors. I have searched everywhere and cannot seem to find the answer.
Positron
Posts: 53
Joined: Mon Mar 31, 2008 11:10 pm

Post by Positron »

Ok, so I did not look everywhere, but I spent a lot of time looking for it with no sucess. I can not even get your example to work. Also, I noticed that it says commandbutton instead of button. Does this matter? I found that button and command button are different things and I am able to use button easier.
jeffslot
Portablizer Extraordinaire
Posts: 853
Joined: Wed Jun 01, 2005 6:26 am
Location: PA
Contact:

Post by jeffslot »

Well, I'm trying to help you the best I can. I actually tested out my code (simple as it was), prior to pasting. So I'm not sure what issue your having.

I guess my point was that in general when attempting to change the color of the item (regardless of item), the backcolor property might be usefull.
Jeff

Below is the complete code I used in Excel to validate your example.

Option Explicit

Private Sub CommandButton1_Click()
CommandButton1.BackColor = vbRed
End Sub
jeffslot
Portablizer Extraordinaire
Posts: 853
Joined: Wed Jun 01, 2005 6:26 am
Location: PA
Contact:

Post by jeffslot »

Well, I'm trying to help you the best I can. I actually tested out my code (simple as it was), prior to pasting. So I'm not sure what issue your having.

I guess my point was that in general when attempting to change the color of the item (regardless of item), the backcolor property might be usefull.
Jeff

Below is the complete code I used in Excel to validate your example.

Option Explicit

Private Sub CommandButton1_Click()
CommandButton1.BackColor = vbRed
End Sub
Positron
Posts: 53
Joined: Mon Mar 31, 2008 11:10 pm

Post by Positron »

Thank you for you suggestion. I got your code to work, but only for the CommandButton. What I need is for it to work for just the button. Also, is there a list of all the commands that VBA accepts? I will try just experimenting changing Commandbutton to just button etc...
jeffslot
Portablizer Extraordinaire
Posts: 853
Joined: Wed Jun 01, 2005 6:26 am
Location: PA
Contact:

Post by jeffslot »

Please clarify what you mean by "Button"? I'm looking at my visual basic toolbar in excel & don't see a "Button". What program are you using? Please be a bit more specific. Thanks.


Thank you for you suggestion. I got your code to work, but only for the CommandButton. What I need is for it to work for just the button. Also, is there a list of all the commands that VBA accepts? I will try just experimenting changing Commandbutton to just button etc...
Positron
Posts: 53
Joined: Mon Mar 31, 2008 11:10 pm

Post by Positron »

Sure, I am using Excel and in the area where you can add various tools I added "Button" which is what I was tought to use. In looking around I found a "CommandButton" tool which looks exactly the same except the "Button" just adds a button that has a grey background, and that I can change the text. The "CommandButton" places a CommandButton that I cannot change the text and has the "Properties" menu when I right click. What I want to do is just to change the Grey background of the "Button" to any color I want. I find that trying to mess around with the "Commandbutton" gets more confusing than I would like.
jeffslot
Portablizer Extraordinaire
Posts: 853
Joined: Wed Jun 01, 2005 6:26 am
Location: PA
Contact:

Post by jeffslot »

Positron,

1st off I do not claim to know everything. I do however use excel / vba a bit.

Forget this button 'please', I can tell from my experience that the command button is a very commonly used control. When you use it you can easily control the text on the button. The Command Button 'Text' will read whatever you put into the 'Caption' Property of it.

By using the properties/methods of any object (In code or in design) is almost always how you interect with it. I really hope this helps you.

However if you are still set on using this 'button' you'll have to go into further intructions on how to add it - I must admit I'm not sure what you're talking about.

Jeff
Sure, I am using Excel and in the area where you can add various tools I added "Button" which is what I was tought to use. In looking around I found a "CommandButton" tool which looks exactly the same except the "Button" just adds a button that has a grey background, and that I can change the text. The "CommandButton" places a CommandButton that I cannot change the text and has the "Properties" menu when I right click. What I want to do is just to change the Grey background of the "Button" to any color I want. I find that trying to mess around with the "Commandbutton" gets more confusing than I would like.
Positron
Posts: 53
Joined: Mon Mar 31, 2008 11:10 pm

Post by Positron »

Ok, Thank you for trying to help me. The Button I am talking about can be found on the "Forms" toolbar. It is labeled "Button" as opposed the the "Command button" which is located under the "Controls" toolbar. I understand that the command button is easier for you to use but I like the button as I do not have to program anything for it. I think I'll just try to use the command button until I get the hang of it.
Post Reply