Determine Cell background color

Ormus

Board Regular
Joined
Nov 1, 2011
Messages
92
I am trying to determine what the background color is of a cell. What I really need is if is cell backround color is not blank. So if A1 background is blank/none, B1 = 0 and if A2 has a yellow background, B2 = 1.

Thanks
 

Some videos you may like

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
How is the cell being coloured? If it's by conditional formatting what are the conditions?
 

Ormus

Board Regular
Joined
Nov 1, 2011
Messages
92

ADVERTISEMENT

Will take a look at it, thanks
 

Sektor

Well-known Member
Joined
May 6, 2011
Messages
2,834
Code:
Function CellColor() As String
    CellColor = GetRGB(ActiveCell.Interior.Color)
End Function

Function GetRGB(RGB As Long) As String

   Dim HexString As String
   Dim r As Double, g As Double, b As Double

   HexString = Hex(RGB)
   HexString = Right(String$(5, "0") & HexString, 6)

   r = CDbl("&H" & Mid$(HexString, 5, 2))
   g = CDbl("&H" & Mid$(HexString, 3, 2))
   b = CDbl("&H" & Mid$(HexString, 1, 2))
   
   GetRGB = "RGB(" & r & ", " & g & ", " & b & ")"

End Function
 

Ormus

Board Regular
Joined
Nov 1, 2011
Messages
92

ADVERTISEMENT

I will check that as well, thanks
 

Ormus

Board Regular
Joined
Nov 1, 2011
Messages
92
Question with this code...

Code:
Function CellColor() As String
    CellColor = GetRGB(ActiveCell.Interior.Color)
End Function

Function GetRGB(RGB As Long) As String

   Dim HexString As String
   Dim r As Double, g As Double, b As Double

   HexString = Hex(RGB)
   HexString = Right(String$(5, "0") & HexString, 6)

   r = CDbl("&H" & Mid$(HexString, 5, 2))
   g = CDbl("&H" & Mid$(HexString, 3, 2))
   b = CDbl("&H" & Mid$(HexString, 1, 2))
   
   GetRGB = "RGB(" & r & ", " & g & ", " & b & ")"

End Function

I placed that in module1. Doing =GetRBG(A1) results in RBG(0, 0, 0). Does not matter what color I have A1 set as a background, does not change. I am doing this correctly?
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Try this:

Code:
Function CellColor(Cell As Range) As String
    CellColor = Cell.Interior.ColorIndex
End Function

and:

=CellColor(A1)
 

Watch MrExcel Video

Forum statistics

Threads
1,122,385
Messages
5,595,869
Members
414,027
Latest member
zippyfrog

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top