Home > Blog

How to display a cell's colour in Excel

It is not possible to directly access the colour of a cell in an Excel worksheet.  Thus you can not write an expression such as “if the cell is red, do this”.   However, it is possible to use a small amount of VBA to generate a “custom function”, which you can then use to do precisely this.

 

Bring up the VBA Window by pressing the <ALT> and <F11> keys.

 

VBA window

Select your file from the list on the left (if it isn’t already selected) and click on the Insert Menu and choose “module”.  Excel will display a blank white space to the right.

Into this area, copy the code below, from Sub to End Sub inclusive.

Function getRGB(rcell) As String
'allows excel to determine the colour of a cell and return the RGB value.
    Dim C As Long
    Dim R As Long
    Dim G As Long
    Dim b As Long
    C = rcell.Interior.Color
    R = C Mod 256
    G = C \ 256 Mod 256
    b = C \ 65536 Mod 256
    getRGB = "RGB(" & R & "," & G & "," & b & ")"
End Function

Now you can call this custom function into any cell in your sheet by entering

=getRGB(A1) to pull the RGB code into the worksheet.

 

Miles Goodchild

Miles Goodchild has been a Program and Programme Planner, PMO lead and Project Manger since 1998.  He enjoys using MS Office, especially Project, Excel and Visio to make life easier and simpler.  In the course of this he created SummaryPro.  In this blog he shares some of the tips and techniques he has learnt over the years in the hope that they will be useful to you.