代码之家  ›  专栏  ›  技术社区  ›  Chris

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

  •  0
  • Chris  · 技术社区  · 6 年前

    它用于检查一行中的多个单元格,如果这些单元格使用条件格式突出显示,则需要执行一个操作—检查单元格中是否有颜色比创建一个包含所有单元格条件的组合条件公式更简单。或者我是这么想的。。

    下面的代码返回一个#值错误,尽管该代码在消息框中工作。。

    Function fillcolour(rng as Range) as variant
    
        fillcolour=rng.Displayformat.Interior.ColorIndex
    
    End Function
    

    2 回复  |  直到 6 年前
        1
  •  1
  •   edu    6 年前

    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单元格中的背景色(即使它来自条件格式)。

    当单元格在新计算过程中发生更改时,不会发生此事件。使用Calculate事件检测范围/单元格中的更改。

        2
  •  0
  •   AAA    6 年前

    link DisplayProperty 在用户定义函数中。它在VBA环境中工作,或从消息框调用时工作,但从工作表调用时不工作。解决方法是删除 .DisplayFormat 根据代码,如下所示:

    Function fillcolour(rng as Range) as variant
    
    fillcolour=rng.Interior.ColorIndex
    
    End Function
    
        3
  •  0
  •   edu    6 年前

    你试过了吗

    Function fillcolour(ring as Range) as variant
    
    fillcolour=rng.DisplayFormat.Interior.Color
    
    End Function
    

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

        4
  •  0
  •   Chris    6 年前

    谢谢你让我帮忙。我现在有一个函数如下:

    Function CFCheck(rng as range) as Integer
    
    CFCheck = rng.FormatConditions.Count
    
    End Function
    

    因此,如果返回的值为>0