如何在Excel中重复function

我在Excel中做了一个函数。 但是在单元更新函数值没有改变之后。 如何在更改单元格后更新函数单元格值

Public Function SumaByColor(ByVal ColorIndex As Range, ByVal TableRange As Range) As Double Dim cell As Range Dim colorIndexNumber As Integer Dim colorSum As Double colorIndexNumber = ColorIndex.Interior.ColorIndex For Each cell In TableRange If cell.Interior.ColorIndex = colorIndexNumber Then colorSum = colorSum + 1 End If Next cell SumaByColor = colorSum End Function 

首先使用Application.Volatile代码开始,然后在Worksheet_SelectionChange写入Me.Calculate

 Public Function SumaByColor(ByVal ColorIndex As Range, ByVal TableRange As Range) As Double Application.Volatile Dim cell As Range Dim colorIndexNumber As Integer Dim colorSum As Double colorIndexNumber = ColorIndex.Interior.ColorIndex For Each cell In TableRange If cell.Interior.ColorIndex = colorIndexNumber Then colorSum = colorSum + 1 End If Next cell SumaByColor = colorSum End Function 

Worksheet_SelectionChange: 在这里输入图像说明