top of page
Writer's pictureMiles Goodchild

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.


The VBA window in Excel
How to open and use the macro area of Exceol

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.

26 views0 comments

Comments


bottom of page