Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如果单元格颜色为XXX&;条件格式_Excel_Vba - Fatal编程技术网

Excel 如果单元格颜色为XXX&;条件格式

Excel 如果单元格颜色为XXX&;条件格式,excel,vba,Excel,Vba,我有一些VB,它允许我根据单元格颜色进行计数 Function COUNTIFCOLOR(rSample As Range, rArea As Range) As Long Dim rAreaCell As Range Dim lMatchColor As Long Dim lCounter As Long lMatchColor = rSample.Interior.Color For Each rAreaCell In rArea If rAreaCell.Interior.Col

我有一些VB,它允许我根据单元格颜色进行计数

Function COUNTIFCOLOR(rSample As Range, rArea As Range) As Long
Dim rAreaCell As Range
Dim lMatchColor As Long
Dim lCounter As Long

lMatchColor = rSample.Interior.Color
For Each rAreaCell In rArea
    If rAreaCell.Interior.Color = lMatchColor Then
        lCounter = lCounter + 1
    End If
Next rAreaCell
CountColorIf = lCounter
End Function
要使用它,你只需说

=COUNTIFCOLOR(A5,J2:J15)
A5也是参考此颜色的单元格,J2:J15是要计数的范围

但是,如果单元格已通过条件格式设置格式,则返回的计数中不会包含此项


现在我被卡住了=/

有一个属性,它可以读取条件格式的颜色。 不幸的是,它不能在UDF中使用,而只能在SUB中使用。 解决方法: 创建一个事件过程,侦听包含以下命令的工作表更改: Target.interior.color=Target.DisplayFormat.interior.color。这会将条件内部颜色复制到内部颜色

将命令Application.volatile添加到现有函数的开头,以便在工作表重新计算时执行该命令

以下是所需的事件过程:

Private Sub Worksheet_Change(ByVal Target As Range)
    Target.Interior.Color = Target.DisplayFormat.Interior.Color
    Application.Calculate
End Sub
applicationon.calculate是必需的,因为您的函数在彩色副本之后的下一个计算周期中生效。 为了节省资源,您可能希望将有效目标范围限制为所需的arrea