【问题标题】:How execute a user defined function automatically in an update?如何在更新中自动执行用户定义的函数?
【发布时间】:2019-07-01 20:18:04
【问题描述】:

我在 Excel 2003 工作表中有这个 VBA 函数:

Function ContarColor(range_data As Range, criteria As Range) As Long
    Dim datax As Range
    Dim xcolor As Long
    xcolor = criteria.Interior.ColorIndex
    For Each datax In range_data
        If datax.Interior.ColorIndex = xcolor Then
            ContarColor = ContarColor + 1
        End If
    Next datax
End Function

我使用该函数计算一个范围内的颜色。

例如我使用:

=ContarColor(D4:AG5;Y31)

其中 D4:AG5 是行的范围,Y31 是引用的行颜色。

如果我添加或删除颜色,并且在公式中不按 Enter,它不会刷新。

如何在编辑范围时自动刷新?

【问题讨论】:

  • 使用Sub Worksheet_ChangeRange(YourRange).Calculate
  • @AntiDrondert, Worksheet_Change 事件处理程序不会被单元格颜色更改触发

标签: excel vba excel-formula


【解决方案1】:

不幸的是,没有跟踪单元格颜色变化的默认事件(假设我们谈论的是正常格式而不是条件格式)。有一些解决方法,但需要循环或类模块 (workarounds)。

此外,默认情况下,Excel VBA 中的 UDF(用户定义函数)不是易失性的。如果您尝试在函数开头添加Application.Volatile,您将看到在更改单元格颜色并按 F9(或工作簿中的任何其他更改)后,该函数应重新计算。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-21
    • 2013-03-29
    • 1970-01-01
    • 2020-04-08
    • 2023-04-10
    • 1970-01-01
    相关资源
    最近更新 更多