简体   繁体   English

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

[英]Excel VBA: Checking if range contains a cell with a specific color

I have the following two user defined functions:我有以下两个用户定义的功能:

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

and

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

The second function does not seem to work when called like ContainsColor(A1:A5,35), what am I missing?第二个函数在像 ContainsColor(A1:A5,35) 这样调用时似乎不起作用,我错过了什么? Thanks.谢谢。

Not so sure why you have 2 Functions , you can just use one: 不太确定为什么要拥有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

Calling it from Excel (in this case will result True, as Cell A1 Interior.ColorIndex = 35) : 从Excel调用(在这种情况下,结果为True,因为单元格A1 Interior.ColorIndex = 35):

在此处输入图片说明

sorry to resurrect an old issue, but I have this exactl formula working in an Excel spreadsheet, and I see that if I change the color of the cell in order to change the output, the result does not automatically update.很抱歉重现一个老问题,但我在 Excel 电子表格中使用了这个精确公式,我发现如果我更改单元格的颜色以更改输出,结果不会自动更新。 I have to F2 in the formula cell and press enter for it to update ?我必须在公式单元格中按 F2 并按 Enter 才能更新? Is there a way to have this aut refresh as the color changes ?有没有办法让这个 aut 随着颜色的变化而刷新?

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM