【问题标题】:Counting merged cells as one with the COUNT IF function while also using other COUNT IF criteria使用 COUNT IF 函数将合并的单元格计数为一个,同时使用其他 COUNT IF 条件
【发布时间】:2019-02-12 03:10:08
【问题描述】:

我使用以下 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 个单元格,同时仍保持颜色编码计数。

任何帮助将不胜感激,谢谢!

【问题讨论】:

  • COUNTIF() 究竟是如何影响这里的?您的代码仅将合并的单元格计为单个单元格,因此不清楚问题是什么......
  • @TimWilliams COUNT IF 函数让我可以根据单元格颜色进行计数,但是,如果一个单元格被合并和着色,则它不会将其计为 1 个单元格,它会将其视为多少个单元格单元格构成合并的单元格。

标签: excel vba countif


【解决方案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

来源:Herethere

【讨论】:

  • 谢谢,这解决了问题:)
猜你喜欢
  • 2022-10-25
  • 2014-01-26
  • 2022-07-01
  • 2022-08-14
  • 2021-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多