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:
Any help would be very much appreciated!![]()
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
Bookmarks