【发布时间】:2015-12-27 09:06:51
【问题描述】:
我有一个 VBA 模块,我上网用条件格式计算单元格。这个模块有一个问题,如果在它计数的范围内,一个单元格是空白的或者没有条件格式规则,它会返回一个错误。模块是:
Function CountCFCells(rng As Range, C As Range)
Dim i As Single, j As Long, k As Long
Dim chk As Boolean, Str1 As String, CFCELL As Range
chk = False
For i = 1 To rng.FormatConditions.Count
If rng.FormatConditions(i).Interior.ColorIndex = C.Interior.ColorIndex Then
chk = True
Exit For
End If
Next i
j = 0
k = 0
If chk = True Then
For Each CFCELL In rng
Str1 = CFCELL.FormatConditions(i).Formula1
Str1 = Application.ConvertFormula(Str1, xlA1, xlR1C1)
Str1 = Application.ConvertFormula(Str1, xlR1C1, xlA1, , ActiveCell.Resize(rng.Rows.Count, rng.Columns.Count).Cells(k + 1))
If Evaluate(Str1) = True Then j = j + 1
k = k + 1
Next CFCELL
Else
CountCFCells = "Color not found"
Exit Function
End If
CountCFCells = j
End Function
当我使用=CountCFCells(A1:A30, B1) 调用此函数时,我希望它忽略任何没有任何条件格式规则或数据(类型为数字)的单元格。忽略范围内没有条件格式规则或数据的任何单元格的最佳方法是什么?
【问题讨论】:
-
到目前为止您尝试过什么?你问最好的办法,有没有想出办法?
-
不,到目前为止我还没有解决方案。只要应用了条件格式规则,即使单元格为黑色,该代码也可以工作。但是,我希望它忽略没有应用条件格式规则的单元格。