gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:29:37 26 4
gpt4 key购买 nike

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

最佳答案

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

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 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 = rng.Cells(1, 1).Interior.Color
Select Case formatType
Case 1
Color = WorksheetFunction.Dec2Hex(colorVal, 6)
Case 2
Color = (colorVal Mod 256) & ", " & ((colorVal \ 256) Mod 256) & ", " & (colorVal \ 65536)
Case 3
Color = rng.Cells(1, 1).Interior.ColorIndex
Case Else
Color = colorVal
End Select
End Function

关于excel - 从 Range.Interior.Color(或任何其他颜色属性)返回 RGB 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24132665/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com