Results 1 to 9 of 9

VBA - Conditionally Change Cell Colour

Threaded View

  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:

    Option Base 1
    
    Sub ColourCells()
    
    Dim ShadedCells As Range
    Dim ColumnX As Range
    Dim Score As Range
    Dim Colours As Range
    
    Set ShadedCells = Range("D2:H16")
    Set ColumnX = Range("D1:H1")
    Set Score = Range("C2:C16")
    Set Colours = Range("J2:J16")
    
    Dim R As Long
    Dim C As Long
    For R = 1 To 15
        For C = 1 To 5
            If ColumnX.Row(C) > Score(R) Then
            ShadedCells(R, C).Interior.ColourIndex = Colours(R).Interior.ColourIndex
            Else
            ShadedCells(R, C).Interior.ColourIndex = RGB(242, 242, 242)
            End If
        Next C
    Next R
    
    End Sub
    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

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. [SOLVED] 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