在Excel中自动运行模块

我在Excel中有一个模块,我需要在工作表中任何单元格更改时自动运行。

现在,它运行,如果我进入编辑模式的单元格中的function,然后按下input或如果我Ctrl+Alt+F9 。 我怎样才能做到这一点? 此外,我不太了解VB或Excel的macros和模块,我也没有写这个代码。

这是我的模块中的代码…

 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 

我只是简单地将Application.Volatile放在macros的开头,当我testing它时,这似乎可以正常工作,但请注意Andy G的上面的纪念:

…更改颜色不会导致重新计算,也不会触发更改事件…更改单元格值对公式结果没有影响

如果你想要改变颜色来触发重新计算,我们需要一个不同的方法。

 Function ColorFunction(rColor As Range, rRange As Range, Optional SUM As Boolean) Dim rCell As Range Dim lCol As Long Dim vResult Application.Volatile 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