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

问题描述 投票:15回答:6

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

excel vba colors rgb
6个回答
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

12
投票

简答:

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

答案很长:

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

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

Determining the RGB Value of a Color


12
投票

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

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

这比使用六角形str的那些快几倍,有些人推荐


2
投票

另一个答案对我不起作用。我找到:

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

它运作正常。


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

0
投票

应该注意,对于十六进制值,如果您要导出为HTML,那么您也会得到怪癖。

理想情况下,您可以从单个颜色创建十六进制字符串,而不是从ColorVal数字返回十六进制。

原因是如果单元格是绿色/蓝色的“纯”颜色,则可以获得一些无效的十六进制数字

RED - RGB(255,0,0)返回'FF' - 它应返回'FF0000'

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

enter image description here

如果您使用它们来创建HTML / CSS颜色输出,那么任何蓝色单元格都会获得RED。

我修改了脚本,根据RGB值组装每个两个字符的十六进制'chunk',一个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) & "&"
© www.soinside.com 2019 - 2024. All rights reserved.