[英]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.