How do I get the corresponding Hex value of an RGB color in Excel/VBA?

后端 未结 5 1166
野的像风
野的像风 2020-12-09 06:01

I\'m trying to set a public const of a color in my VBA code. Normally, I can use:

Dim BLUE As Long
BLUE = RGB(183, 222, 232)

However, ther

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-09 06:41

    Function GetRGB(ByVal cell As Range) As String
    
    Dim R As String, G As String
    Dim b As String, hexColor As String
    hexCode = Hex(cell.Interior.Color)
    
    'Note the order excel uses for hex is BGR.
    b = Val("&H" & Mid(hexCode, 1, 2))
    G = Val("&H" & Mid(hexCode, 3, 2))
    R = Val("&H" & Mid(hexCode, 5, 2))
    
    GetRGB = R & ":" & G & ":" & b
    End Function
    

    note that excel RGB values are backwards (BGR)

提交回复
热议问题