I run across a post from 2020 Automate Font Color Depending on Cells Background Color Brightness and found a response by @Tragamor that was ‘almost’ exactly what I was looking for. Tragamor posted code in reference to creating a function to test if the cell color was dark or not.
Being VERY new to VBA I’m trying to figure out how that function can be made to apply to more than one cell at a time with reference to using it in conditional formatting. I’ve attempted to nest other functions within IsDark to reference individual cells such as ADDRESS(ROW(),COLUMN(),4) but in this case I constantly get #VALUE! errors.
My goal is to find a way to use the function in conditional formatting to select cells based on if the color of the cell is dark and then set the non-fill formatting options to other complimentary colors. Ideally I would like to be able to specify the colors to be used in a separate sheet in the workbook that I call settings. I presently have names defined for colors used in conditional formatting rules and I would like to use the same configuration to dictate the formatting options of the rules in conditional formatting using this function.
However, as written, IsDark can only be set to reference a single cell which has to be manually typed and doesn’t support function nesting. The original code from the post if you don’t want to follow the link above is as follows:
Public Function IsDark(ByRef Cell As Range) As Boolean
'If Cell.Cells.Count > 1 Then Exit Function
Dim Blue As Long, Green As Long, Red As Long, PerceivedColor As Double
Red = Cell.Interior.Color Mod 256
Green = Cell.Interior.Color 256 Mod 256
Blue = Cell.Interior.Color 65536 Mod 256
PerceivedColor = 0.299 * (Red ^ 2) + 0.587 * (Green ^ 2) + 0.114 * (Blue ^ 2)
IsDark = IIf(Sqr(PerceivedColor) > 127.5, False, True)
End Function
Thanks in advance for any help.
JerryS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.