我在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
答案 0 :(得分:1)
首先使用Application.Volatile
代码开头,然后在Me.Calculate
Worksheet_SelectionChange
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