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.


Solution 1:

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