Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.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 - Fatal编程技术网

如何在excel中重复函数

如何在excel中重复函数,excel,vba,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.C

我用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
代码开头,然后在
工作表中写入
Me.Calculate
\u selection change

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
工作表\u选择更改:

AFAIK,更改某些单元格中的颜色不会触发计算。您需要使用
F9
强制重新计算,或者最终您可以截取
工作表\u SelectionChange
。例如,我在工作表\u SelectionChange
Me中写了什么。计算