【问题标题】:Count colored cells by conditional formatting (greater than/lesser than)通过条件格式计算彩色单元格(大于/小于)
【发布时间】:2019-11-16 02:07:56
【问题描述】:

我有由条件格式 GREATER THAN / LESSER THAN 格式化的列。我想要一个函数来获取范围和引用的单元格。

我从网上尝试了几个 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 给了我 NO-COLOR。我认为无法检测到颜色,但我还能做什么?有什么想法吗?

【问题讨论】:

  • 不要使用 UDF,而是使用具有用于条件格式的逻辑的公式。
  • 请缩进您的代码,使其更具可读性和更易于调试。
  • Rubberduck 有一个缩进来帮助你

标签: excel vba conditional-formatting


【解决方案1】:

我有一个应用程序可以用颜色来做一些时髦的事情。我写这篇文章是为了检查我是否已经照顾了一行。代码的关键位是:

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) 会怎样?也许这会简化您的检查以使其正常工作?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    • 2017-07-09
    • 1970-01-01
    相关资源
    最近更新 更多