excel - 自定义excel公式函数UDF来计数条件格式

标签 excel vba function udf

有人运行过可以实际使用条件格式的函数吗?

有一些适用于 kutools 和 albebits 的插件,但它们不是基于公式的(您必须手动选择所有内容)

我找到了这个,但仅适用于手动格式化

Function ColorFunction(rColor As Range, rRange As Range, Optional SUM As Boolean)
Dim rCell As Range
Dim lCol As Long
Dim vResult
lCol = rColor.Interior.ColorIndex
If SUM = True Then
   For Each rCell In rRange
    If rCell.Interior.ColorIndex = lCol Then
            vResult = WorksheetFunction.SUM(rCell) + vResult
    End If
   Next rCell
Else
    For Each rCell In rRange
    If rCell.Interior.ColorIndex = lCol Then
            vResult = 1 + vResult
    End If
   Next rCell
End If
ColorFunction = vResult
End Function

最佳答案

继@Jeeped 和@Comintern 之后...

这对我有用 - 一个简化的示例:

Function WrapCountReds(rRange)
    WrapCountReds = rRange.Parent.Evaluate("CountReds(" & _
                          rRange.Address(False, False) & ")")
End Function

'can't call this directly from a worksheet but can be called via evaluate
Public Function CountReds(rRange As Range)

    Dim rCell As Range
    Dim vResult

    For Each rCell In rRange
      If rCell.DisplayFormat.Interior.ColorIndex = 3 Then
             vResult = 1 + vResult
      End If
    Next rCell

    CountReds = vResult
End Function

工作表使用示例:

=WrapCountReds("A1:A100")

关于excel - 自定义excel公式函数UDF来计数条件格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38750643/

相关文章:

VBA代码冲突

c - 按名称处理函数

Python 控制函数的图形输出

vba - 将 'yyyymmdd hhmmss' 转换为 'mm/dd/yy hh:mm'

excel - 在 Excel 中使用变量作为 COUNTIF

excel - 如何检查值是否仅存在于一个或其他范围内,否则会出错?

vba - 使 VBE 助手在使用其他办公应用程序的功能/命令时显示

excel - 在 VBA 中使用 curl 上传图像

c - 什么是运行时约束?

Excel COUNTIF,其中满足条件范围的单元格数不连续