Excel 按条件格式(大于/小于)计数彩色单元格

Excel 按条件格式(大于/小于)计数彩色单元格,excel,vba,conditional-formatting,Excel,Vba,Conditional Formatting,我有一些列的格式为大于/小于的条件格式。我想要一个函数,它接受范围和引用的单元格 我从网上尝试了几个UDF,它们要么不计算任何颜色,要么计算每个有条件着色的单元格,而不是应该计算颜色的引用单元格。例如: Function COUNTConditionColorCells(CellsRange As Range, ColorRng As Range) Dim Bambo As Boolean Dim dbw As String Dim CFCELL As Range

我有一些列的格式为大于/小于的条件格式。我想要一个函数,它接受范围和引用的单元格

我从网上尝试了几个UDF,它们要么不计算任何颜色,要么计算每个有条件着色的单元格,而不是应该计算颜色的引用单元格。例如:

Function COUNTConditionColorCells(CellsRange As Range, ColorRng As Range)
    Dim Bambo As Boolean
    Dim dbw As String
    Dim CFCELL As Range
    Dim CF1 As Single
    Dim CF2 As Double
    Dim CF3 As Long

    Bambo = False

    For CF1 = 1 To CellsRange.FormatConditions.Count
        If CellsRange.FormatConditions(CF1).Interior.ColorIndex = ColorRng.Interior.ColorIndex Then
            Bambo = True
            Exit For
        End If
    Next CF1

    CF2 = 0
    CF3 = 0

    If Bambo = True Then
        For Each CFCELL In CellsRange
            dbw = CFCELL.FormatConditions(CF1).Formula1
            dbw = Application.ConvertFormula(dbw, xlA1, xlR1C1)
            dbw = Application.ConvertFormula(dbw, xlR1C1, xlA1, , ActiveCell.Resize(CellsRange.Rows.Count, CellsRange.Columns.Count).Cells(CF3 + 1))

            If Evaluate(dbw) = True Then CF2 = CF2 + 1

            CF3 = CF3 + 1
        Next CFCELL
    Else
        COUNTConditionColorCells = "NO-COLOR"
        Exit Function
    End If

    COUNTConditionColorCells = CF2
End Function

这个UDF给我没有颜色。我认为颜色无法检测,但我还能做什么?有什么想法吗?

我有一个应用程序,它可以用色彩做一些有趣的事情。我写这篇文章是为了检查我是否已经照看了一排人。代码的关键部分是:

sht.Cells(theRow, theColumn).Select    

If Selection.Interior.Color = eYellow Or Selection.Interior.Color = eGreen Then 'make sure we're not overwriting a row.
    Stop
End If
在我之前定义过的eYellow和eGreen

Private Const eYellow As Long = rgbYellow
Private Const eGreen As Long = rgbLightGreen
棘手的是,只有在我首先选择了有问题的单元格时,我才能使支票生效


我认为现在的情况是,您的if语句没有得到满足,因为您检查了错误的属性。如果您尝试不使用.FormatConditions(CF1)?也许这会简化您的检查以使其正常工作?

与其使用自定义项,不如使用带有条件格式逻辑的公式。请缩进您的代码,使其更易于阅读和调试。有一个缩进来帮你