我更正了我的代码,但它没有完全回答您的问题,因为它不包括条件格式。
更正的代码:
Function my_Count_Color(Arg1 As Range, Farbe As Integer) As Integer
Dim elem As Variant
For Each elem In Arg1
If elem.Interior.ColorIndex = Farbe Then
my_Count_Color = my_Count_Color + 1
End If
Next elem
End Function
要检查,请使用以下公式:
Function GetColor(R As Range) As Integer
GetColor = R.Interior.ColorIndex
End Function
但是,如果此解决方案不能满足您的要求,我会为您提供一个不同的功能建议,它应该满足您的期望。唯一发生的变化是从 ColorIndex 到 RGB。对应于 ColorIndex = 43 = RGB (146,208,80) 的值。我将它们作为可选替换为公式,或者更确切地说是 2。现在您可以键入 My_Count_Color_2 = (G1: G10) 或 My_Count_Color_2 = (G1: G10; 146; 208; 80)
这是我的代码:
Function my_Count_Color_2(rng As Range, Optional R As Integer = 146, Optional G As Integer = 208, Optional B As Integer = 80) As Integer
Dim elem As Variant
For Each elem In rng
If (rng.Parent.Evaluate("DFColor(""" & elem.Address & """)") = RGB(R, G, B)) = True Then
my_Count_Color_2 = my_Count_Color_2 + 1
End If
Next elem
End Function
Public Function DFColor(addr)
DFColor = Range(addr).DisplayFormat.Interior.Color
End Function