如果删除行,自定义函数将给出错误

时间:2019-04-23 12:07:26

标签: excel vba

我有一个自定义公式,可以对表中具有特定BackColor的所有单元格求和(感谢StackOverflow!)

Function SumByColor(CellColor As Range, rRange As Range)
Dim cSum As Long
Dim ColIndex As Integer
ColIndex = CellColor.Interior.ColorIndex
For Each cl In rRange
  If cl.Interior.ColorIndex = ColIndex Then
    cSum = WorksheetFunction.Sum(cl, cSum)
  End If
Next cl
SumByColor = cSum
End Function

这被插入到Excel表的 TotalsRowRange 中。

我创建了几个自定义按钮,用于为表中的每个员工添加新行和删除行,每个按钮都有3行,其中包含小时,百分比和工资公式。

如果我添加新行,则 SumByColor 功能将完美运行,并且新创建的行将自动包含在总和中(它将较浅的绿色行相加)。

但是当我删除行时, SumByColor 函数不会更新正确性,并显示“ VALUE”错误。 单元格上的简单双击和确定即可将公式重置为正常操作。 我已经尝试过对TotalsRowRange对象使用Calculate方法,但是不走运。

Here's the error after deleting rows

Here's the formula, GENDIR is the name of the column

0 个答案:

没有答案