Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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
用VBA计算Excel中具有相同内部颜色的单元格_Vba_Excel_Conditional Formatting - Fatal编程技术网

用VBA计算Excel中具有相同内部颜色的单元格

用VBA计算Excel中具有相同内部颜色的单元格,vba,excel,conditional-formatting,Vba,Excel,Conditional Formatting,我正在尝试计数重复的细胞。在一列中,我首先使用条件格式查找重复项。我用黄色RGB255255255突出显示我的单元格。但是,当我尝试计数高亮显示的单元格时,由于某种原因,我的vba没有检测到它们 如果我在条件格式上突出显示单元格,我的函数将检测颜色并正确计数。关于为什么我不能检测条件格式有什么想法吗 Function colorCount(arr As Variant, r As Integer, G As Integer, B As Integer) As Integer Dim x As V

我正在尝试计数重复的细胞。在一列中,我首先使用条件格式查找重复项。我用黄色RGB255255255突出显示我的单元格。但是,当我尝试计数高亮显示的单元格时,由于某种原因,我的vba没有检测到它们

如果我在条件格式上突出显示单元格,我的函数将检测颜色并正确计数。关于为什么我不能检测条件格式有什么想法吗

Function colorCount(arr As Variant, r As Integer, G As Integer, B As Integer) As Integer
Dim x As Variant
Dim xRow As Integer
Dim xCol As Integer

colorCount = 0
For Each x In arr.Cells

xRow = x.Row
xCol = x.Column

If Cells(xRow, xCol).Interior.Color = RGB(r, G, B) Then
colorCount = colorCount + 1
End If
Next

End Function

谢谢。

事实证明,条件格式实际上并不像“传统”格式那样为单元格着色

有两种方法可以解决这个问题:

使用VBA检测重复项:

' dupe check requires cells to be highighted
YourWB.Sheets(1).Range("A" & Rows.Count).End(xlUp).Select

' now check that row for dupes, and flag each dupe with some formatting
Dim d As Object, e
Set d = CreateObject("scripting.dictionary")
For Each e In Intersect(Columns(ActiveCell.Column), ActiveSheet.UsedRange)
    If e.Value <> vbNullString Then
        If Not d.exists(e.Value) Then d(e.Value) = 1 Else _
            e.Font.ColorIndex = 4
    End If
Next
检测应用的条件颜色


这有点复杂,我使用了代码

为什么不使用应用于CF的逻辑进行计数?CF不设置Interior.Color,因此这种方法永远不会运行。仅供参考,CellsxRow,xCol.Interior.Color与x.Interior.Color在这里相同,因此您的代码可以短得多,即使由于@teylyn概述的原因,它无法工作。不幸的是,我没有对我的副本使用任何逻辑。我正在使用excel内置的条件格式。但是,发送的@pnuts链接可能有用。非常感谢。