gpt4 book ai didi

打印单元格颜色(ColorIndex 或 RGB)的 Excel 公式

转载 作者:行者123 更新时间:2023-12-01 18:26:59 35 4
gpt4 key购买 nike

在 Excel 中是否有一个公式可以检索单元格的 ColorIndex(或 RGB)?

我发现了以下功能:

CELL(info_type, the_cell)

记录here ,但没有任何单元格颜色的引用信息。

这是一个颜色信息,但对我来说没有用。其实是这样描述的:

"color" The value 1 if the cell is formatted in color for negative values; otherwise returns 0 (zero).

有什么想法吗?

另外,我发现执行此操作的 VBA 属性是 Cell.Interior.Color 但实际上我没有使用宏,而是使用简单的 Excel 公式。有没有办法用公式模拟 VBA 函数?

最佳答案

这里为您提供一些小功能。在工作表中,按 Alt-F11 进入 VBA 编辑器,插入新模块,粘贴以下代码,返回工作表并按名称使用它们,如 =FillColor(A1)

前两个是 promise 的“3-liners”,为字体和背景颜色提供十进制值 - 虽然不是很有用

第二对将十进制数转换为 RGB 并返回格式为 N, N, N 的字符串

第三对是数组公式 - 选择一行中的 3 个单元格,输入公式并按 Ctrl+Shift+ 输入以获取 3 个相邻单元格中的 RGB 数值

Function FillColor(Target As Range) As Variant
FillColor = Target.Interior.Color
End Function

Function FontColor(Target As Range) As Variant
FontColor = Target.Font.Color
End Function

Function FillColorRGB(Target As Range) As Variant
Dim N As Double

N = Target.Interior.Color
FillColorRGB = Str(N Mod 256) & ", " & Str(Int(N / 256) Mod 256) & ", " & Str(Int(N / 256 / 256) Mod 256)
End Function

Function FontColorRGB(Target As Range) As Variant
Dim N As Double

N = Target.Font.Color
FontColorRGB = Str(N Mod 256) & ", " & Str(Int(N / 256) Mod 256) & ", " & Str(Int(N / 256 / 256) Mod 256)
End Function

Function FillColorRGBArray(Target As Range) As Variant
Dim N As Double, A(3) As Integer

N = Target.Interior.Color
A(0) = N Mod 256
A(1) = Int(N / 256) Mod 256
A(2) = Int(N / 256 / 256) Mod 256
FillColorRGBArray = A
End Function

Function FontColorRGBArray(Target As Range) As Variant
Dim N As Double, A(3) As Integer

N = Target.Font.Color
A(0) = N Mod 256
A(1) = Int(N / 256) Mod 256
A(2) = Int(N / 256 / 256) Mod 256
FontColorRGBArray = A
End Function

注意事项:更改单元格的颜色不会通过上述函数/公式启动重新计算,因为一般情况下,对单元格重新着色不会驱动重新计算。您必须使用 Ctrl+Alt+Shift+F9 手动启动完全重新计算

关于打印单元格颜色(ColorIndex 或 RGB)的 Excel 公式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14281966/

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