如何在Excel/VBA中获取RGB颜色对应的十六进制值?

问题描述 投票:0回答:7

我正在尝试在我的 VBA 代码中设置颜色的公共常量。通常,我可以使用:

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

但是,由于 RGB 函数,无法将其公开 const。我使用在线转换器将此 RGB 值转换为十六进制,然后我得到了 B7DEE8

使用:

BLUE = &HB7DEE8

产生完全不同的颜色。我认为这实际上可能是 RGBA 颜色,我尝试过 B7DEE8__ 并得到了非常接近的颜色(最后一位数字是 B8),但我想知道如何实际找到正确的值。

注意:我真的不需要代码将其转换为十六进制,我只需要知道如何找到它,因为我在 Excel 工作表上使用了五种常量颜色,并且我想设置它们。

vba colors excel rgba
7个回答
12
投票

您必须将字节反转为顺序

BLUE = &HE8DEB7

获取正确的颜色值。


12
投票

出现明显反转的原因是 RGB() 函数实际上创建了一个 BGR 值。

更具体地说,红色字节是低位字节,蓝色字节是高位字节(或至少四个字节中的第三个)。

在立即窗口中尝试此示例:

x = RGB(255, 0, 128) ' full red, half blue
? hex(x)
8000FF

x = RGB(128, 0, 255) ' half red, full blue
? hex(x)
FF0080

请注意,“全”字节(255 或 FF)和“半满”字节(128 或 80)最终位于每个结果的相反两侧。这就是为什么您需要以与您期望获得相同值相反的顺序指定十六进制常量。

此外,无需使用在线转换器。 Hex() 函数提供给定数字的十六进制值,Int 将采用十六进制格式的字符串并返回十进制值:

? Int("&hff0000") 
 16711680

更新:

因此,要使用此信息创建十六进制常量,您只需在上面的立即窗口中运行 RGB() 和 Hex() 语句(按 Ctrl+G 打开和关闭它),然后使用生成的十六进制值作为您的十六进制值。持续的。如果值的长度小于 6 位,您可以在左侧填充零,但这在技术上是不必要的:

x = RGB(183, 222, 232)
? "Public Const MyBlue = &h" & hex(x)
Public Const MyBlue = &hE8DEB7

然后将最后一行复制到您的代码中。


4
投票

好的,以下将采用 Excel 2010 中单元格的颜色并提供有效的十六进制代码:

Public Function getHexCol(a As Range)

' In excel type in for example getHexCol(A1) to get the hexcode of the color on     A1.
Dim strColour As String
Dim hexColour As String
Dim nColour As Long
Dim nR As Long, nB As Long, nG As Long

strColour = a.Interior.Color
If Len(strColour) = 0 Then Exit Function

nColour = Val(strColour) ' convert string to decimal number
hexColour = Hex(nColour) ' convert decimal number to hex string
While Len(hexColour) < 6 ' pad on left to 6 hex digits
hexColour = "0" & hexColour
Wend

nB = CLng("&H" & Mid(hexColour, 1, 2))
nG = CLng("&H" & Mid(hexColour, 3, 2))
nR = CLng("&H" & Mid(hexColour, 5, 2))

getHexCol = Hex(RGB(nB, nG, nR))
End Function

2
投票
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

注意Excel RGB值是向后的(BGR)


0
投票

这是另一个在 MS Access 中也适用的函数,它可以解释逆向 RGB 顺序:

Function VBA_RGB_To_HEX(iRed As Integer, iGreen As Integer, iBlue As Integer) As String
    Dim sHex As String
    sHex = "#" & VBA.Right$("00" & VBA.Hex(iBlue), 2) & VBA.Right$("00" & VBA.Hex(iGreen), 2) & VBA.Right$("00" & VBA.Hex(iRed), 2)
    VBA_RGB_To_HEX = sHex
End Function

0
投票

抱歉我来晚了。

工作表上的按钮等 ActiveX 元素可以更改其背景和字体颜色。它们使用类似于 &H00B5752F& 的特定十六进制格式。它基于蓝、绿、红顺序,而不是 RGB 类型的红、绿、蓝顺序

如果您知道 RGB 值并希望转换为 ActiveX 颜色十六进制格式,可以使用以下过程。

Sub RGB_to_ActiveX_HEX_Color()
'       This macro allows the user to input numeric RGB values
'       to get the HEX format for Activex element Hex Colors
'
'       Result will be printed in the immediate window
'
'       Example: RGB(47,117,181)  =  &H00B5752F&


Dim mySplitArr
Dim myRGB_numbers As Variant
Dim newStr As String
Dim commaTally As Long
        
        myRGB_numbers = InputBox("Enter RGB values separated by commas ", "RGB to HEX")
        
        On Error GoTo error_Handler_main
        mySplitArr = Split(myRGB_numbers, Chr(44))
        
        For i = LBound(mySplitArr) To UBound(mySplitArr)
            If Not IsNumeric(mySplitArr(i)) Or mySplitArr(i) < 0 Or mySplitArr(i) > 255 Then GoTo error_Handler_main
        Next i
        
        If UBound(mySplitArr) < 2 Then GoTo error_Handler_main
        
        Debug.Print "RGB(" & mySplitArr(0) & ", " & mySplitArr(1) & ", " & mySplitArr(2) & ")  =  " & "&H00" & VBA.Right$("00" & VBA.Hex(mySplitArr(2)), 2) & VBA.Right$("00" & VBA.Hex(mySplitArr(1)), 2) & VBA.Right$("00" & VBA.Hex(mySplitArr(0)), 2) & "&"

Exit Sub
error_Handler_main:
    MsgBox "There was an error in your input" & vbNewLine & vbNewLine & _
            "Be sure to separate RGB values with commas." & vbNewLine & vbNewLine & _
            "example:   123,255,0" & vbNewLine & vbNewLine & _
            "Also values must be between 0 and 255 for each color"
    Exit Sub
    

End Sub

-1
投票

我测试了这段代码,不能真正遵循霍华德的答案

Dim rd, gr, bl As Integer
rd = 183
gr = 222
bl = 232
BLUE = RGB(rd, gr, bl)
hexclr = Format(CStr(Hex(rd)), "00") +
Format(CStr(Hex(gr)), "00") + 
Format(CStr(Hex(bl)), "00")
MsgBox hexclr 'B7DEE8
© www.soinside.com 2019 - 2024. All rights reserved.