Search code examples
excelvbacolorshexrgb

How to get HEX color value from cell color?


I want to get HEX color value from cell (range) color.

The following links are giving wrong results.

https://stackoverflow.com/a/24216193/13075931

https://stackoverflow.com/a/50479544/13075931

You can find detailed explanation in the following picture.

Detailed explanation

Update: Thanks for good answers but I have found better solutions in the following link.

https://excelribbon.tips.net/T010180_Determining_the_RGB_Value_of_a_Color.html


Solution

  • Simpler

    Function GetColorInHex(cc As Range) As String
      Dim s As String
      s = Hex(cc.Cells(1).Interior.Color)
      s = String(6 - Len(s), "0") & s
      GetColorInHex = Right(s, 2) & Mid(s, 3, 2) & Left(s, 2)
    End Function