如何在excel中重复功能

时间:2017-02-24 10:00:30

标签: excel vba

我在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

1 个答案:

答案 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

<强> Worksheet_SelectionChange: enter image description here