返回条件格式化单元格内部颜色的函数

问题描述 投票:0回答:5

我想要一个返回条件格式单元格颜色索引值的函数。

它用于检查一行中的多个单元格,如果它们使用条件格式突出显示,则需要执行操作 - 检查单元格中是否有颜色更简单,而不是创建覆盖单元格的组合条件公式所有细胞的状况。或者我是这么想的..

下面的代码返回 #VALUE 错误,尽管该代码在消息框中工作..

Function fillcolour(rng as Range) as variant

    fillcolour=rng.Displayformat.Interior.ColorIndex

End Function

期望看到返回的颜色索引值,但得到#VALUE

excel vba colors conditional-formatting
5个回答
1
投票

要使用更改事件:

Private Sub Worksheet_Change(ByVal Target As Range)

    Application.EnableEvents = False
    Call fillcolour(Cells(1, 1))
    Application.EnableEvents = True

End Sub



Function fillcolour(ring As Range) As Variant

    fillcolour = ring.DisplayFormat.Interior.Color
    Cells(1, 2) = fillcolour

End Function

每次更改单元格的内容时,它都会显示单元格 B1 中的背景颜色(即使它来自条件格式)。

但是,此事件返回一个目标,其中包含导致事件的行和列(Target.Row、Target.Column 等)。您可以使用此目标来检测直接在单元格上所做的更改。

当单元格在新计算期间发生变化时,不会发生此事件。使用计算事件来检测范围/单元格的变化。


0
投票

请参阅此链接了解您遇到此问题的原因。您不能在用户定义的函数中使用

DisplayProperty
。它在 VBA 环境中或从消息框调用时有效,但在从工作表调用时无效。解决方法是从代码中删除
.DisplayFormat
,如下所示:

Function fillcolour(rng as Range) as variant

fillcolour=rng.Interior.ColorIndex

End Function

0
投票

你尝试过吗?:

Function fillcolour(ring as Range) as variant

fillcolour=rng.DisplayFormat.Interior.Color

End Function

将其分配给单元格的事件更改


0
投票

感谢 Asger 在这里提供帮助。我现在的功能如下:

Function CFCheck(rng as range) as Integer

CFCheck = rng.FormatConditions.Count

End Function

因此,如果返回的值>0,它就可以触发操作

进一步测试后发现,这个功能只是区分有 CF 的细胞和没有 CF 的细胞,这很有帮助,但在我的情况下却没有。我需要一些东西来查看具有 CF 的单元格,但 CF 尚未应用颜色,因为条件尚未满足。


0
投票

我想我会根据上面的代码添加一个最近的项目,我用它来查找 CF 单元的颜色,可能会对某人有所帮助,感谢上面的内容。

单元 M1 - 保存颜色代码

单元格 N1 - 范围内最左上角的单元格

单元格 O1 - 范围内最右下角的单元格

M2 中的输出 - 范围内 CF 颜色的计数

将强制更新按钮链接到 在模块中:

    Global start_cell As String
    Global end_cell As String
    Global find_colour As Double
    Global found_colour As Double
    Global totalN As Integer
    Global Setup As Boolean
    Global x1 As String
    Global x2 As String
    Global y1 As String
    Global y2 As String

Function Setup_Now()

    start_cell = (Range("N1").Value)
    end_cell = (Range("O1").Value2)
    find_colour = (Range("M1").Value2)
    
    x1 = convertrow(start_cell)
    x2 = convertrow(end_cell)
    y1 = convertcol(start_cell)
    y2 = convertcol(end_cell)

End Function

Function convertrow(rng As String)

    convertrow = Range(rng).Row

End Function

Function convertcol(rng As String)

    convertcol = Range(rng).Column

End Function

Function fillcolour(rng As Range) As Variant

    fillcolour = rng.DisplayFormat.Interior.Color

End Function

Function reset()
    
    Application.EnableEvents = True
 
End Function

Sub Calculate()
    
    Call reset
    Range("M2").Value = "=2+2"
    
End Sub

在工作表中:

Private Sub Worksheet_Calculate()

    Call reset
    DoEvents
    
    totalN = 0
    x = 0
    y = 0
    
    Application.EnableEvents = False
    
    If Setup = False Then
        Call Setup_Now
        Setup = True
    Else
    'do nothing
    End If
    
        For x = x1 To x2
            For y = y1 To y2
                found_colour = fillcolour(Cells(x, y))
                If found_colour = find_colour Then: totalN = totalN + 1
            Next
        Next


    
    Debug.Print totalN, x, y
    Range("M2").Value = totalN
        
    Application.EnableEvents = True
    
  End Sub
© www.soinside.com 2019 - 2024. All rights reserved.