Excel VBA:检查范围是否包含具有特定颜色的单元格

时间:2017-02-07 18:10:25

标签: excel excel-vba vba

我有以下两个用户定义的函数:

Function GetFillColor(Rng As Range) As Long
    GetFillColor = Rng.Interior.ColorIndex
End Function

Function ContainsColor(Rng As Range, Clr As Long) As Boolean
    ContainsColor = False
    For Each c In Rng
        If GetFillColor(c) = Clr Then
            ContainsColor = True
            Exit For
        End If
    Next
End Function

第二个函数在调用ContainsColor(A1:A5,35)时似乎不起作用,我缺少什么?感谢。

1 个答案:

答案 0 :(得分:1)

不确定为什么你有2 Functions,你可以使用一个:

Function ContainsColor(Rng As Range, Clr As Long) As Boolean

    Dim c As Range
    ContainsColor = False
    For Each c In Rng
        If c.Interior.ColorIndex = Clr Then
            ContainsColor = True
            Exit For
        End If
    Next

End Function

从Excel调用它(在这种情况下将生成True,如单元格A1 Interior.ColorIndex = 35):

enter image description here