+ Reply to Thread
Results 1 to 9 of 9

VBA - Conditionally Change Cell Colour

  1. #1
    Registered User
    Join Date
    09-11-2015
    Location
    Calgary, AB
    MS-Off Ver
    2010
    Posts
    5

    VBA - Conditionally Change Cell Colour

    I am struggling to get some VBA code to run properly (or even run at all, frankly) - it keeps flagging errors, and no matter what I try to adjust it, it will not execute. I have successfully tinkered with code in the past, but this is the first time I've attempted to write something more-or-less from scratch with only basic sections pulled off the internet (as I could not find anything designed to do this sort of thing), so I may be missing something quite fundamental.

    The intent is to change the colour of cells in a table based on whether the value of the first column exceeds the number in the row header. I have provided a snapshot of my spreadsheet for reference:

    \1

    If the value in column C is less than or equal to the value in row 1, the corresponding cell colours in D2:H16 should match the colour in column J. If the value is greater, the cell should be light grey. I have a chart with formatting tied to the cell colours, so I need to change the colour of the cell itself, not apply a conditional formatting rule.

    Here is my attempt at coding:

    Please Login or Register  to view this content.
    Any help would be very much appreciated!
    Attached Images Attached Images
    Last edited by parsesdata; 09-11-2015 at 10:29 PM. Reason: Added code tags; Fixed Image

  2. #2
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    2,760

    Re: VBA - Conditionally Change Cell Colour

    Please use code tags when posting code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here
    Click the * Add Reputation button in the lower left hand corner of this post to say thanks.

    Don't forget to mark this thread SOLVED by going to the "Thread Tools" drop down list above your first post and choosing solved.

  3. #3
    Registered User
    Join Date
    09-11-2015
    Location
    Calgary, AB
    MS-Off Ver
    2010
    Posts
    5

    Re: VBA - Conditionally Change Cell Colour

    Thanks for the tip!

    I'm not sure why the image isn't showing. I'll try to fix that as well...

  4. #4
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,946

    Re: VBA - Conditionally Change Cell Colour

    Hi, welcome to the forum

    You dio know you can do this with regular CF, right?
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  5. #5
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    2,760

    Re: VBA - Conditionally Change Cell Colour

    It would be helpful if you would actually post a spreadsheet, we can then use your data example and the colors you want. Please also put a sample of the grey color you want.

    If the value in column C is less than or equal to the value in row 1
    Which value, you have 5 numbers in row 1?

    What is the significance of the 1's and in one case 2's in the colored rows?

    Why do the color bands only go part way across some of the rows?

  6. #6
    Registered User
    Join Date
    09-11-2015
    Location
    Calgary, AB
    MS-Off Ver
    2010
    Posts
    5

    Re: VBA - Conditionally Change Cell Colour

    Thanks for the welcome! I've been playing with this for a while, with no success, and figured a different strategy might be in order.

    Conditional formatting was absolutely my first thought, and I did try it. The problem is that the code I am using to format the chart based on this data uses "Interior.Color" to get the cell colour - that returns the base colour of the cell, not the colour applied by conditional formatting. Looking at various articles online, it seems like it would be much easier to change the cell colours with VBA than to identify the colour applied to each of the cells by conditional formatting. If it would only work...

  7. #7
    Registered User
    Join Date
    09-11-2015
    Location
    Calgary, AB
    MS-Off Ver
    2010
    Posts
    5

    Re: VBA - Conditionally Change Cell Colour

    The values in the coloured / shaded cells feed into a donut chart - they just define the size of the slices, and are not important to the VBA conditional formatting.

    The idea is that if the value in the top row (1-5) is less than or equal to the value in the left hand column (ranking from 1-5), the cell at the intersection of that row and column gets assigned the accent colour from the corresponding row in column J. Think of it like a bar chart - if it says "4" in the left hand column, I want a coloured bar that is 4 cells long. The remaining cells are RGB(242,242,242), per the code.

  8. #8
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    2,760

    Re: VBA - Conditionally Change Cell Colour

    Okay fine. How about the sample sheet?

  9. #9
    Registered User
    Join Date
    09-11-2015
    Location
    Calgary, AB
    MS-Off Ver
    2010
    Posts
    5

    Re: VBA - Conditionally Change Cell Colour

    VBA - Colour Coding Cells.xlsm

    See attached. It really isn't anything more than what I had posted previously, but hopefully a sheet to manipulate will get some more discussion going.

    I also tried using direct cell references, which also did not seem to work. Here is the code:

    Please Login or Register  to view this content.
    Thanks again for your help!

+ 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. Replies: 3
    Last Post: 09-03-2022, 07:48 AM
  2. Change colour of cells based on another cell's colour (Not value)
    By LTrain89 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-25-2013, 08:44 PM
  3. [SOLVED] Macro to change text colour of adjacent cell to the same text colour as target cell
    By kayoke in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 06-28-2012, 04:18 AM
  4. Replies: 2
    Last Post: 06-07-2011, 07:00 AM
  5. Clear contents based on colour of a conditionally formatted cell
    By mit400 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-24-2011, 10:14 AM
  6. Colour change column chart based on cell colour
    By Alice21 in forum Excel General
    Replies: 11
    Last Post: 04-05-2011, 10:10 AM
  7. change a cell background colour to my own RGB colour requirements
    By Stephen Doughty in forum Excel General
    Replies: 4
    Last Post: 06-16-2006, 08:15 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