【发布时间】:2015-01-20 15:49:58
【问题描述】:
我已经编写了一个 UDF 来计算特定颜色和特定线条样式的单元格,我将发布整个函数:
Function CountTime(rData As Range, cellRefColor As Range) As Variant
Dim indRefColor As Long
Dim cellCurrent As Range
Dim cntRes As Variant
Application.Volatile
cntRes = 0
indRefColor = cellRefColor.Cells(1, 1).Interior.Color
For Each cellCurrent In rData
If indRefColor = cellCurrent.Interior.Color Then
cntRes = cntRes + 1
End If
If cellCurrent.Borders(xlDiagonalUp).LineStyle <> xlNone Then
cntRes = cntRes + 0.5
End If
Next cellCurrent
CountTime = cntRes
End Function
现在,我遇到的问题是,当rData 中的一个单元格的颜色或线条属性发生变化时,公式不会自动计算。
我添加了Application.Volatile,并且我还尝试通过Worksheet_Change 子触发计算,但是这不起作用,因为Excel 似乎没有考虑更改单元格/工作表的颜色。
当用户更改rData中单元格的颜色或线条属性时,有什么方法可以使单元格自动计算和更新?
编辑——已解决 非常感谢 ignotus,ChangeSelection 变通方法对我的目的来说已经足够了。没想到。 背景信息也很方便,非常感谢。
【问题讨论】:
-
我想解决方案是您需要检查范围内的每个单元格是否有颜色变化。并在更改时调用您的 UDF
-
简短的回答(您有以下几种解决方法)是否定的。格式化应该用于反映数据,而不是作为数据。 ;)
-
我就是这么说的。 :-) 我建议让用户在单元格中输入一个值,通过条件格式更改颜色。然后计算会容易得多。但是不,人们习惯于使用颜色。变更管理:-)