Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/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
Excel 值更改后更新单元格函数_Excel_Vba_Function_Module - Fatal编程技术网

Excel 值更改后更新单元格函数

Excel 值更改后更新单元格函数,excel,vba,function,module,Excel,Vba,Function,Module,我有一个VBA,可以计算彩色单元格的数量。VBA模块被视为一个单元。但是,该函数仅在我单击单元格函数并按ENTER键时运行。更改单元格值不会自动运行该函数。 选项中也启用了公式的自动更新 以下是我的VBA: 函数ColorFunction(rColor作为范围,rRange作为范围,可选求和作为布尔值) 变暗rCell As范围 暗淡的lCol尽可能长 迪姆·弗雷苏特 lCol=rColor.Interior.ColorIndex 如果SUM=True,则 对于安排中的每个rCell 如果rC

我有一个VBA,可以计算彩色单元格的数量。VBA模块被视为一个单元。但是,该函数仅在我单击单元格函数并按ENTER键时运行。更改单元格值不会自动运行该函数。 选项中也启用了公式的自动更新

以下是我的VBA:

函数ColorFunction(rColor作为范围,rRange作为范围,可选求和作为布尔值)
变暗rCell As范围
暗淡的lCol尽可能长
迪姆·弗雷苏特
lCol=rColor.Interior.ColorIndex
如果SUM=True,则
对于安排中的每个rCell
如果rCell.Interior.ColorIndex=lCol,则
vResult=工作表函数.SUM(rCell,vResult)
如果结束
下一个rCell
其他的
对于安排中的每个rCell
如果rCell.Interior.ColorIndex=lCol,则
vResult=1+vResult
如果结束
下一个rCell
如果结束
ColorFunction=vResult

结束函数
我认为您需要设置
Application.Volatile

详情如下:


希望有帮助。

您可以使用一些解决方法

在相关图纸代码窗格中,放置以下代码

Option Explicit

Dim myColor As Long '<--| variable to store the "preceeding" color

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim coloredRng As Range

    If myColor > 0 Then '<--| if a "colored" cell had been selected in the "preceeding" selection, then store its color
        Me.Calculate '<--| trigger calculation and, by its effects, all functions with 'Application.Volatile'
        myColor = 0 '<--| reset "preceeding" color to zero. it'll be set to a vaild color if user has currently selected a "colored" cell
    End If

    Set coloredRng = Range("J70:J73") '<--| set the range whose color change must "trigger" 'ColorFunction()' (change "J70:J73" to your actual "colored" cells addresses)
    If Not Intersect(Target, coloredRng) Is Nothing Then myColor = Target.Interior.Color '<--| if current selection belongs to the "colored" range then store its color to trigger 'ColorFunction()' cells as soon as the user leaves the current selection
End Sub
选项显式

Dim myColor只要“0那么”谢谢你的回复。。。Application.Valatile仅在值a更改时起作用:(背景单元格颜色改变时不需要。)有什么解决方法吗?我甚至不介意是否有更新单元格的按钮。您是否尝试过以下方法:Ctrl+Alt+F9重新计算所有打开工作簿中的所有工作表(完全重新计算)Shift+Ctrl+Alt+F9重建依赖关系树并执行完全重新计算recalculation@KADAragorn,你熬过了吗?