从Range.Interior.Color(或任何其他颜色属性)返回RGB值

时间:2014-06-10 04:05:31

标签: excel vba colors rgb

我试图逐渐将单元格的背景颜色更改为黑色,我发现Range.Interior.Color方法返回一个看似随意的Long。查看MSDN上的文档,几乎没有关于此数字代表什么的信息。有没有办法从这个长度返回RGB值。我实际上需要RGB(红色,绿色,蓝色)功能的反面。

7 个答案:

答案 0 :(得分:38)

“任意”数字是RGB值(B * 256 ^ 2 + G * 256 + R)的数学组合和十六进制颜色值到十进制数(基数16到基数10)的转换,具体取决于你想以哪种方式看待它。只是不同的基础。下面是我在为Excel编写的XLAM addin文件中使用的方法。这种方法已经多次派上用场了。我已将文档包含在我的addin文件中。

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'   Function            Color
'   Purpose             Determine the Background Color Of a Cell
'   @Param rng          Range to Determine Background Color of
'   @Param formatType   Default Value = 0
'                       0   Integer
'                       1   Hex
'                       2   RGB
'                       3   Excel Color Index
'   Usage               Color(A1)      -->   9507341
'                       Color(A1, 0)   -->   9507341
'                       Color(A1, 1)   -->   91120D
'                       Color(A1, 2)   -->   13, 18, 145
'                       Color(A1, 3)   -->   6
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function Color(rng As Range, Optional formatType As Integer = 0)     As Variant
    Dim colorVal As Variant
    colorVal = Cells(rng.Row, rng.Column).Interior.Color
    Select Case formatType
        Case 1
            Color = Hex(colorVal)
        Case 2
            Color = (colorVal Mod 256) & ", " & ((colorVal \ 256) Mod 256) & ", " & (colorVal \ 65536)
        Case 3
            Color = Cells(rng.Row, rng.Column).Interior.ColorIndex
        Case Else
            Color = colorVal
    End Select
End Function

答案 1 :(得分:12)

简答:

此功能没有内置功能。你必须编写自己的函数。

长答案:

从Interior.Color属性返回的long是我们习惯在html中查看颜色的典型十六进制数的十进制转换,例如“66FF66”。此外,常量xlNone(-4142)可以传递给设置单元格,在背景中没有颜色,但是这些单元格从RGB(255, 255, 255)属性标记为白色Get。知道了这一点,我们就可以编写一个返回一个或所有相应RGB值的函数。

幸运的是,Allan Wyatt先生在这里做到了这一点!

Determining the RGB Value of a Color

答案 2 :(得分:12)

很高兴看到怀亚特先生使用快速的RGB颜色方法

R = C Mod 256
G = C \ 256 Mod 256
B = C \ 65536 Mod 256

比使用左右中间的六角形str快几倍 一些人建议

答案 3 :(得分:2)

另一个答案对我不起作用。我发现:

R = C And 255
G = C \ 256 And 255
B = C \ 256 ^ 2 And 255

它运作正常。

答案 4 :(得分:1)

这是给猫剥皮的另一种方式

'
' Type definition in declarations
'
Type RGBcolor
    r As Long
    g As Long
    b As Long
End Type
'
' Inverse RGB function
'
Function GetRGB(ByVal x As Long) As RGBcolor
    With GetRGB
        .r = x Mod 256
        x = x \ 256
        .g = x Mod 256
        x = x \ 256
        .b = x Mod 256
    End With
End Function
'
'  Sub to test the GetRGB function
'
Sub test(x As Long)
Dim c As RGBcolor
c = GetRGB(x) ' returns RGB values: c.r, c.g, c.b
Debug.Print "Original", "Red", "Green", "Blue", "Recombined value"
Debug.Print x, c.r, c.g, c.b, RGB(c.r, c.g, c.b)
End Sub
'
'
***** IMMEDIATE WINDOW *****
test 1000
Original      Red           Green         Blue          Recombined value
 1000          232           3             0             1000 

答案 5 :(得分:0)

Mark Ba​​lhoff的VBA脚本运行正常。所有的功劳归他所有。

如果您还想获取条件格式的单元格的颜色代码/索引,则可以这样修改代码:

'----------------------------------------------------------------
'   Function            Color
'   Purpose             Determine the Background Color Of a Cell
'   @Param rng          Range to Determine Background Color of
'   @Param formatType   Default Value = 0
'                       0   Integer             color of cell, not considering conditional formatting color
'                       1   Hex                 color of cell, not considering conditional formatting color
'                       2   RGB                 color of cell, not considering conditional formatting color
'                       3   Excel Color Index   color of cell, not considering conditional formatting color
'                       4   Integer             "real" visible color of cell (as the case may be the conditional formatting color)
'                       5   Hex                 "real" visible color of cell (as the case may be the conditional formatting color)
'                       6   RGB                 "real" visible color of cell (as the case may be the conditional formatting color)
'                       7   Excel Color Index   "real" visible color of cell (as the case may be the conditional formatting color)
'   Usage               Color(A1)      -->   9507341
'                       Color(A1, 0)   -->   9507341
'                       Color(A1, 1)   -->   91120D
'                       Color(A1, 2)   -->   13, 18, 145
'                       Color(A1, 3)   -->   6
'-----------------------------------------------------------------
Function Color(rng As Range, Optional formatType As Integer = 0) As Variant
    Dim colorVal As Variant
    Select Case formatType
        Case 0 To 3
            colorVal = Cells(rng.Row, rng.Column).Interior.Color
        Case 4 To 7
            colorVal = Cells(rng.Row, rng.Column).DisplayFormat.Interior.Color
    End Select
    Select Case formatType
        Case 0
            Color = colorVal
        Case 1
            Color = Hex(colorVal)
        Case 2
            Color = (colorVal Mod 256) & ", " & ((colorVal \ 256) Mod 256) & ", " & (colorVal \ 65536)
        Case 3
            Color = Cells(rng.Row, rng.Column).Interior.ColorIndex
        Case 4
            Color = colorVal
        Case 5
            Color = Hex(colorVal)
        Case 6
            Color = (colorVal Mod 256) & ", " & ((colorVal \ 256) Mod 256) & ", " & (colorVal \ 65536)
        Case 7
            Color = Cells(rng.Row, rng.Column).DisplayFormat.Interior.ColorIndex
    End Select
End Function

答案 6 :(得分:0)

应该注意,对于十六进制值,如果要导出到HTML,也会有一些古怪之处。

理想情况下,您将根据各个颜色创建一个十六进制字符串,而不是从ColorVal数字返回一个十六进制。

原因是,如果单元格是“纯”颜色(如绿色/蓝色),则可能会得到无效的十六进制数字

红色-RGB(255,0,0)返回'FF'-应该返回'FF0000'

蓝色-RGB(0,0,255)返回'FF00000'-应该返回'0000FF'

enter image description here

如果使用它们创建HTML / CSS颜色输出,则任何蓝色单元格都将变成红色。

我修改了脚本,以基于RGB值来组装每两个字符的十六进制“块”,而UDF仅填充了前导0并返回了一个字符的输出(希望如果您正在阅读,可以做出类似的东西)

Color = ZeroPad(Hex((colorVal Mod 256)), 2) & ZeroPad(Hex(((colorVal \ 256) Mod 256)), 2) & ZeroPad(Hex((colorVal \ 65536)), 2)

-编辑:忘记包含UDF的代码...

Function ZeroPad(text As String, Cnt As Integer) As String
'Text is the string to pad
'Cnt is the length to pad to, for example  ZeroPad(12,3) would return a string '012' , Zeropad(12,8) would return '00000012' etc..
Dim StrLen As Integer, StrtString As String, Padded As String, LP As Integer


StrLen = Len(Trim(text))


    If StrLen < Cnt Then

        For LP = 1 To Cnt - StrLen

            Padded = Padded & "0"

        Next LP

    End If

ZeroPad = Padded & Trim(text)

ENDOF:


End Function

顺便说一句-如果您希望在表单编辑器中显示十六进制代码(除了普通的HTML十六进制颜色外,它具有自己的标准)

    Case 4  ' ::: VBA FORM HEX :::
    Color = "&H00" & ZeroPad(Hex((colorVal \ 65536)), 2) &  ZeroPad(Hex(((colorVal \ 256) Mod 256)), 2) & ZeroPad(Hex((colorVal Mod 256)), 2) & "&"