+ Reply to Thread
Results 1 to 11 of 11

need a command button to change / unchange the colour of specified cells

  1. #1
    Registered User
    Join Date
    12-19-2013
    Location
    Scotland
    MS-Off Ver
    Microsoft Excel for Microsoft 365 64-bit
    Posts
    11

    need a command button to change / unchange the colour of specified cells

    Hi,

    I'm a complete novice with macros so please forgive me in advance!

    Hope someone can help me with the VBA code required.

    Please see the attached file...


    I've assigned a command button to a macro but I cant get it to perform as required.

    What I require the macro to do is this - When the command button is clicked, specified cells change from no fill colour (which should be the state when the excel file is opened) to a yellow fill colour. When I click the command button again, the cells return to no fill colour.

    Does that make sense?

    Below is the code I have. I know its a load of rubbish, but to be honest I haven't a clue what I'm doing!

    Please Login or Register  to view this content.
    Any help would be much appreciated!

    Best Regards,
    Cal
    Attached Files Attached Files
    Last edited by fingermouse; 01-09-2014 at 05:54 AM.

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,623

    Re: need a command button to change / unchange the colour of specified cells

    Check out such modification of your macro:
    Please Login or Register  to view this content.
    I added static (not volatile) variable mycolor which is assigned to property .color of the "selected" cells interior.
    Note that I got rid of .Select .Activate and Selection.

    This part
    Please Login or Register  to view this content.
    could be written much shorter way as:
    Please Login or Register  to view this content.
    Last edited by Kaper; 01-09-2014 at 06:29 AM.

  3. #3
    Registered User
    Join Date
    12-19-2013
    Location
    Scotland
    MS-Off Ver
    Microsoft Excel for Microsoft 365 64-bit
    Posts
    11

    Re: need a command button to change / unchange the colour of specified cells

    That's perfect Kaper, much appreciated!!!

  4. #4
    Registered User
    Join Date
    12-19-2013
    Location
    Scotland
    MS-Off Ver
    Microsoft Excel for Microsoft 365 64-bit
    Posts
    11

    Re: need a command button to change / unchange the colour of specified cells

    If I wanted to modify Kaper's code so that when the command button was clicked, some specified cells (B11,B17,D11,D15,D21) would be coloured yellow but others (B13, D17)would be coloured orange - how would the code be modified?

    I require the same functionality (the cell state is initially 'no fill' but when clicked, specified cells change to yellow and orange, then when clicked again returns to no fill)

    I'm not sure where I would add the code when introducing the 'orange' variable, as its no longer either 'no fill' or 'yellow'.

    Does that make sense?

    Any help would be much appreciated!

  5. #5
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,623

    Re: need a command button to change / unchange the colour of specified cells

    Yet another modification:

    Please Login or Register  to view this content.
    note that I used constants rgbYellow rgbOrange and rgbWhite instead of "strange numbers" and changed type of static constant to boolean.

  6. #6
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: need a command button to change / unchange the colour of specified cells

    Maybe:

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    12-19-2013
    Location
    Scotland
    MS-Off Ver
    Microsoft Excel for Microsoft 365 64-bit
    Posts
    11

    Re: need a command button to change / unchange the colour of specified cells

    Thanks Guys, these both work!

    I really appreciate the advice...

  8. #8
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,623

    Re: need a command button to change / unchange the colour of specified cells

    Good point John - most of the code from registered macro is an overhead. And with simple colors colorindex is as good as color (introduced only in ex2007 if I remember)

    I would just use only one IF instruction:
    Please Login or Register  to view this content.

  9. #9
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: need a command button to change / unchange the colour of specified cells

    Even a better point. I'm still operating in the dark ages though 2003.

  10. #10
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,623

    Re: need a command button to change / unchange the colour of specified cells

    Heh, I am even more "prehistoric".
    In the association I am working part time aside from my main job I use Excel 2000 and ... its

  11. #11
    Registered User
    Join Date
    01-09-2014
    Location
    Chandigarh
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: need a command button to change / unchange the colour of specified cells

    Welcome to the Board!

    Create a command button and then put in some code like this:

    Code:
    Private Sub CommandButton1_Click()
    selection.interior.colorindex = 6
    End Sub
    I mean this is as simple as it gets, but it limits it to one color and I don't know if you want to uncolor cells, but I think you want more than this? This will go in the code for the command button.

    Hope that helps.

+ 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. Chaniging cells font colour after using command button
    By ncaravela in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-17-2013, 04:59 PM
  2. using a command button to change certain cells font colour.
    By ncaravela in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-17-2013, 10:08 AM
  3. [SOLVED] Change Command Button Colour Based on Cell
    By adam.hewitt5 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-08-2012, 09:43 AM
  4. command button that changes colour of selected cells
    By BLRITCHIE in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-22-2008, 07:33 PM
  5. Using a Command Button to colour cells
    By Wibs in forum Excel General
    Replies: 1
    Last Post: 08-24-2006, 05:42 PM

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