我使用以下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个单元格,同时仍然保持颜色编码计数。
任何帮助将不胜感激,谢谢!
处理合并的单元格而不是单独合并单元格然后将它们全部加在一起:
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