How are cell colors in Excel set to the hexadecimal RGB value that they contain?

I have seem a lot of questions and answers (and search engine results) about how to set a cell color based upon its value (with Conditional Formatting), but no answers showing how to set a cell color to its value. For example if a cell's value is #FFFFFF, the cell's color would be black.

1 Answer

This is not native Excel functionality. You would need VBA to build that.

Conceptually, split the cell content out into the three hex numbers that make up the code.

Convert each Hex number into a decimal number and use these three numbers in an RGB() function to set the cell fill.

enter image description here

Private Sub Worksheet_Change(ByVal Target As Range) If Not Intersect(Range("A1"), Target) Is Nothing Then varRed = WorksheetFunction.Hex2Dec(Mid(Range("A1"), 2, 2)) varGreen = WorksheetFunction.Hex2Dec(Mid(Range("A1"), 4, 2)) varBlue = WorksheetFunction.Hex2Dec(Mid(Range("A1"), 6, 2)) With Target.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .Color = RGB(varRed, varGreen, varBlue) .PatternTintAndShade = 0 End With End If End Sub 
0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like