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
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
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
 
Upvote 0
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?
 
Upvote 0
Try this:

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

and:

=CellColor(A1)
 
Upvote 0

Forum statistics

Threads
1,214,402
Messages
6,119,301
Members
448,885
Latest member
LokiSonic

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
Back
Top