在Excel中,是否存在检索单元格的ColorIndex(或RGB)的公式?
我找到了以下功能:
CELL(info_type, the_cell)
记录了here,但它没有任何关于单元格颜色的参考信息。
这是color
信息,但对我来说没用。
实际上,它描述如下:
"color"
如果单元格的颜色为负值,则值为1;否则返回0(零)。
有什么想法吗?
另外,我发现这样做的VBA属性是Cell.Interior.Color
但实际上我并没有使用宏,而是使用简单的Excel公式。有没有办法用公式模拟VBA函数?
答案 0 :(得分:4)
以下是一些小功能。从工作表中,按 Alt - F11 进入VBA编辑器,插入新模块,粘贴以下代码,返回工作表并按名称使用它们,例如=FillColor(A1)
前两个是承诺的“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
答案 1 :(得分:0)
以下函数将显示所选单元格的RGB值。
Function CellColorValue(CellLocation As Range)
Dim sColor As String
Application.Volatile
'Retrieve hex value into string sColor
sColor = Right("000000" & Hex(CellLocation.Interior.Color), 6)
'Return the string Version e.g. 255,255,255 RGB color value found in
'Excel cell. Use in built worksheet function to convert Hex to Decimal
'Use string function to separate Hex string into three parts
CellColorValue = Application.WorksheetFunction.Hex2Dec(Right(sColor, 2)) & "," & application.WorksheetFunction.Hex2Dec(Mid(sColor, 3, 2)) & "," & Application.WorksheetFunction.Hex2Dec(Left(sColor, 2))
End Function
答案 2 :(得分:0)
请尝试使用以下
所做的更改:请参阅代码中的评论
<强>模块强>
Public Function Performance_Message(NonPreferredAvg As Single _
, NonPreferredAvgname As String _
, PreferredAvg As Single _
, PreferredAvgname As String _
, Optional Outputtype As String _
) As Variant
Dim performancemessage As String
Dim averagedifference As Single
Dim stravgdif As String
Dim cellcolor As String
averagedifference = Abs(NonPreferredAvg - PreferredAvg)
stravgdif = FormatPercent(averagedifference, 2)
Select Case PreferredAvg
Case Is < NonPreferredAvg
performancemessage = PreferredAvgname & " Is " & stravgdif & " Less Than " & NonPreferredAvgname
cellcolor = 4 '"green" 'Changes made
Case Is = NonPreferredAvg
performancemessage = PreferredAvgname & " Equals " & NonPreferredAvgname
cellcolor = 6 '"yellow" ''Changes made
Case Is > NonPreferredAvg
performancemessage = PreferredAvgname & " Is " & stravgdif & " Greater Than " & NonPreferredAvgname
cellcolor = 5 '"blue" 'Changes made
Case Else
performancemessage = "Something Bad Happened"
End Select
If Outputtype = "color" Then
Performance_Message = cellcolor
Else
Performance_Message = performancemessage
End If
End Function
<强>工作表强>
Private Sub Worksheet_Change(ByVal Target As Range)
Dim myColor As Double
myColor = Target.Value ''Changes made
Call SetPerformancecolor(Target, myColor)
End Sub
Private Sub SetPerformancecolor(Target As Range, myColor As Double)
Target.Interior.ColorIndex = myColor ''Changes made
End Sub