0

我尝试计算一个范围内具有特定颜色的所有单元格。在我的情况下,颜色是绿色(ColorIndex:43)。单元格的颜色取决于条件格式。

到目前为止,我的代码如下:

Function Count_color(range_data As Range, Farbe As Integer) As Integer
    Dim datax As Range
    Dim index As Integer
    For Each datax In range_data
        index = datax.DisplayFormat.Interior.ColorIndex
        If index = Farbe Then
            Count_color = Count_color + 1
        End If
    Next datax
End Function

在单元格中,我应用该函数,我收到错误消息“值”。我将非常感谢您的帮助。

4

1 回答 1

0

我更正了我的代码,但它没有完全回答您的问题,因为它不包括条件格式。

更正的代码:

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
于 2020-07-30T14:28:03.703 回答