【发布时间】:2019-02-18 18:14:30
【问题描述】:
我正在尝试编写一个 UDF 来计算具有条件格式的单元格的数量。我写了以下子,就像一个魅力:
Sub SumCountByConditionalFormat()
Dim cellrngi As Range
Dim cntresi As Long
cntresi = 0
Set cellrngi = Sheets("Sheet3").Range("I2:I81")
For Each i In cellrngi
If i.DisplayFormat.Interior.Color <> 16777215 Then
cntresi = cntresi + 1
End If
Next i
end sub
我尝试使用以下代码将其转换为 UDF:
Function CountCellsByColor1(rData As Range) As Long
Dim cntRes As Long
Application.Volatile
cntRes = 0
For Each cell In rData
If cell.DisplayFormat.Interior.Color <> 16777215 Then
cntRes = cntRes + 1
End If
Next cell
CountCellsByColor1 = cntRes
End Function
但是,当我尝试 UDF 时,我得到了 #VALUE!回来。我真的不知道为什么,任何帮助将不胜感激。
【问题讨论】:
-
@SJR 谢谢!不幸的是,现在不是#VALUE!我得到一个 0。
-
在测试函数时,只需从代码中删除
DisplayFormat就可以得到正确的值。确保选择了正确的单元格并使用F9运行计算以更新单元格 -
我在 excel 2010 中使用 Power Query。您认为这可能导致错误吗?
-
您始终可以在函数中使用您的 CF 标准。