我已使用以下VBA模块使用COUNT IF函数根据单元格颜色对列中的项目列表进行计数:
Function CountCcolor(range_data As range, criteria As range) As Long
Dim datax As range
Dim xcolor As Long
xcolor = criteria.Interior.ColorIndex
For Each datax In range_data
If datax.Interior.ColorIndex = xcolor Then
CountCcolor = CountCcolor + 1
End If
Next datax
End Function
但是,我的列表中还包含合并的单元格,虽然上面的函数有效,但它会将合并的单元格计数为组成合并的单元格的单元格的数量(因此,例如,由3个常规单元格组成的合并的单元格将计为3在列表中)。如果可能的话,我需要一种方法来将合并的单元格计数为1个单元格,同时仍保持颜色编码计数。
任何帮助将不胜感激,谢谢!
答案 0 :(得分:1)
分别处理合并的单元格和未合并的单元格,然后将它们全部添加在一起:
Function CountCcolor(range_data As Range, criteria As Range) As Long
Application.Volatile
Dim datax As Range
Dim xcolor As Long
Set D1 = CreateObject("scripting.dictionary")
xcolor = criteria.Interior.ColorIndex
For Each datax In range_data
If datax.Interior.ColorIndex = xcolor Then
If datax.MergeCells Then
D1(datax.MergeArea.Address) = datax.MergeArea.Address
Else
CountCcolor = CountCcolor + 1
End If
End If
Next datax
CountCcolor = CountCcolor + D1.Count
End Function