+ Reply to Thread
Results 1 to 12 of 12

Change the Color of One Command Button with Click of Another

  1. #1
    Registered User
    Join Date
    01-15-2016
    Location
    Dallas, TX
    MS-Off Ver
    2007
    Posts
    65

    Change the Color of One Command Button with Click of Another

    Hi All,

    Say I have 3 command buttons on a sheet (in real life I have 84). I created a class module which uses one simple sub, executing a command based on which button is clicked. Subsequently, the clicked button changes color. If you click another button, that button changes color. My challenge: when the second button is clicked, I want the first button to resume its original color.

    For Simplicity's sake, I created a workbook with 3 command buttons and applied only the code needed to change the color. Please check out the code below, and the attached workbook and see if you can come up with a solution to my challenge.

    General Module:
    Please Login or Register  to view this content.
    Class Module:
    Please Login or Register  to view this content.
    Thanks!
    Attached Files Attached Files

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Change the Color of One Command Button with Click of Another

    Do you want all the other buttons to change colour so the only button that's red is the last one that was clicked?

    Or are the buttons grouped in some way?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    01-15-2016
    Location
    Dallas, TX
    MS-Off Ver
    2007
    Posts
    65

    Re: Change the Color of One Command Button with Click of Another

    The Buttons are grouped in the Class_Init. so when the code in the Class Module is executed, it only sees the clicked button.

    As it is, if you click a button once, it changes to red. Click it again and it goes back to gray. The problem is that you can click button 1 and make it red, and click button 2 and make it red at the same time. I'd like to make it so that if you click button two, only button 2 will be red... all others will be gray.

    Is it possible with a class module, to work on "unclicked" buttons?

    Thanks!

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Change the Color of One Command Button with Click of Another

    If you declare Buttons3 you could try something like this.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    01-15-2016
    Location
    Dallas, TX
    MS-Off Ver
    2007
    Posts
    65

    Re: Change the Color of One Command Button with Click of Another

    I get a type mismatch in the For

  6. #6
    Registered User
    Join Date
    01-15-2016
    Location
    Dallas, TX
    MS-Off Ver
    2007
    Posts
    65

    Re: Change the Color of One Command Button with Click of Another

    Off to a meeting... Might be a while before I check Back... Thanks for working on it though!!

  7. #7
    Registered User
    Join Date
    01-15-2016
    Location
    Dallas, TX
    MS-Off Ver
    2007
    Posts
    65

    Re: Change the Color of One Command Button with Click of Another

    Hi Norie...

    I get a number of errors trying to do this. Perhaps the difficulty is in how I declare Buttons3?? Did it actually work for you when you put the code in the sample workbook I attached? (Assuming of course you did). If so, What did you declare Buttons3 as. I have tried everything that I think makes sense...

    Any assistance is greatly appreciated.

    Ray

  8. #8
    Registered User
    Join Date
    01-15-2016
    Location
    Dallas, TX
    MS-Off Ver
    2007
    Posts
    65

    Cool Re: Change the Color of One Command Button with Click of Another

    Ha! Okay... I used the same method used in the General Module to create the Class Module - Inside the Class Module... Works like a charm. See the code below for giggles

    General Module:
    Please Login or Register  to view this content.
    Class Module:
    Please Login or Register  to view this content.
    So this one is solved... Thanks for the help!

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Change the Color of One Command Button with Click of Another

    No problem.

    On my first attempt I actually had it working in the workbook you uploaded with the code I posted and Buttons3 declared as Public.

    Unfortunately I started mucking about with it and it stopped working.

  10. #10
    Registered User
    Join Date
    01-15-2016
    Location
    Dallas, TX
    MS-Off Ver
    2007
    Posts
    65

    Re: Change the Color of One Command Button with Click of Another

    Awesome!... I'm going to go back to your original and play with it... I am a newb, and didn't think to declare it as Public.

    Thank You again

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Change the Color of One Command Button with Click of Another

    The thing is though, I tried it again and I couldn't get it to work.

    Wait a minute, I just downloaded the original file and made the changes I suggested and it's working.

    I'm confused.

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Change the Color of One Command Button with Click of Another

    Right I've got it working.

    I've also added a call to Class_Init in the workbook open event, and it appears to work.

    When I originally tried adding that call was when things stopped working.

    Anyways see the attached file.
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Click oval option button to fill/unfill color, also change border color to match fill
    By Rocky2013 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-07-2015, 08:29 PM
  2. [SOLVED] click on a color button and after button click it will color entire row of cell you click
    By swade730 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-11-2014, 06:06 PM
  3. [SOLVED] command button color changed while cells value change and else...
    By iqballud in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-10-2013, 08:24 AM
  4. use a command button to click other command buttons
    By fcharl9 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-20-2013, 08:48 PM
  5. [SOLVED] Command color change upon a click
    By macrofan2012 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 10-19-2012, 09:28 AM
  6. Command Button to change the subsequent rows with each click
    By sand_electro in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-12-2012, 04:13 PM
  7. [SOLVED] change the face color of an Excel command button
    By Chris Leah in forum Excel General
    Replies: 4
    Last Post: 07-12-2005, 10:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1