[英]VBA Self-Function returns #VALUE! Error on cell, while in Function window returns the actual value correctly
我在下面写的函数是一个范围,我有一些条件格式(对于字体颜色),另一个单元格范围用于比较颜色。 功能是计算大范围内具有与一个单元格范围相同的字体颜色的单元格数。
Function CountColor(rng As Range, clr As Range) As Integer
Dim c As Range
Dim a As Integer
a = 0
For Each c In rng
If c.DisplayFormat.Font.Color = clr.Font.Color Then
a = a + 1
End If
Next
CountColor = a
End Function
现在,问题是 - 在函数窗口中,实际结果是正确的,而在单元格本身,我得到#VALUE! 错误。
以下代码适用于我,但不适用于条件格式:
Option Explicit
Function CountColor(rng As Range, clr As Variant) As Variant
Dim c As Range
Dim a As Integer
a = 0
For Each c In rng
If c.Font.color = clr.Font.color Then
a = a + 1
End If
Next c
CountColor = a
End Function
如果我只是改变字体颜色,除了条件格式,它的工作原理。 但由于某种原因它不起作用,否则。
正如多个人在评论中提到的那样 - DisplayFormat属性在用户定义的函数中不起作用。 因此,如果您删除.DisplayFormat.
从你的功能它应该按预期工作。
Function CountColor(rng As Range, clr As Range) As Integer
Dim c As Range
Dim a As Integer
a = 0
For Each c In rng
If c.Font.Color = clr.Font.Color Then
a = a + 1
End If
Next
CountColor = a
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.