Excel VBA中的颜色值 [英] Color values in Excel VBA

查看:163
本文介绍了Excel VBA中的颜色值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了一些搜索,但找不到答案(或者可能在错误的位置寻找:/)

I've done a bit of searching for this one and cannot find the answer (or maybe looking in the wrong places :/)

我正在尝试查找excel VBA中使用的颜色值的参考.我有几行简单的代码行,它们会更改所选单元格中的背景颜色(在这种情况下,我正在使用一个简单的宏来调用代码).

I am trying to find a reference for the colour values used in excel VBA. I have a few simple lines of code which change the background colour in a selected cell (in this case I am using a simple macro to invoke the code).

我遇到了由宏记录器生成的该值:

I have a come across this value which was generated by the macro recorder:

Selection.Interior.Color = 15773696

我的首选是使用RGB(上面示例的RGB等效值为128、128、128),我一直在尝试找出宏记录器从何处获得此值.

My preference is to use RGB (the RGB equivalent of the above example is 128, 128, 128) and I have been trying find out where the macro recorder got this value from.

很抱歉,这是一个常见问题解答.

Apologies if this is a FAQ.

TIA

推荐答案

要转换

Option Explicit

Public Sub ConvertColor()

    Convert2RGB 15773696

End Sub

Public Sub Convert2RGB(ByVal c As Long)

    Dim R As Long, G As Long, B As Long

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

    'Or
    'R = clr And 255
    'G = clr \ 256 And 255
    'B = clr \ 256 ^ 2 And 255

    Debug.Print "R:" & R & " G:" & G & " B:" & B    '15773696 -> R:0 G:176 B:240

    Debug.Print RGB(R, G, B)                        'R:0 G:176 B:240 -> 15773696

End Sub


从SO中:返回RGBRange.Interior.Color(或任何其他Color属性)中的值

...数字是RGB值的数学组合(B * 256 ^ 2 +G * 256 + R)并将十六进制颜色值转换为十进制数(从16到10的基数)...

...我们通常的十六进制数的十进制转换用于查看html中的颜色,例如"66FF66" ...


更多详细信息(外部链接):确定颜色的RGB值

这篇关于Excel VBA中的颜色值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆