【问题标题】:Count Cells By Colour with COUNTIF使用 COUNTIF 按颜色计数单元格
【发布时间】:2016-08-16 03:21:11
【问题描述】:

我有以下宏 CountCellsByColor (ORIGNAL BELOW) 但是我想修改它,以便它按颜色和单元格的特定文本来计算单元格。

例如:该范围有 5 个不同的名称,它们都用不同的颜色着色。我希望宏只计算与参考单元格具有相同名称和颜色的单元格。即“弗雷德”“黄色”细胞的数量

原始公式如下:

Function CountCellsByColor(rData As Range, cellRefColor As Range) As Long
    Dim indRefColor As Long
    Dim cellCurrent As Range
    Dim cntRes As Long

    Application.Volatile
    cntRes = 0
    indRefColor = cellRefColor.Cells(1, 1).Interior.Color
    For Each cellCurrent In rData
        If indRefColor = cellCurrent.Interior.Color Then
            cntRes = cntRes + 1
        End If
    Next cellCurrent

    CountCellsByColor = cntRes
End Function

【问题讨论】:

  • 感谢杰里米,您的原始评论有效,即 - 如果 indRefColor = cellCurrent.Interior.Color 和 cellCurrent.Value = searchtext 然后 - 我只是没有输入它最初写的。非常感谢您的帮助。真的很感激

标签: excel vba


【解决方案1】:

改变应该就够了

If indRefColor = cellCurrent.Interior.Color Then

If (indRefColor = cellCurrent.Interior.Color) AND (cellRefColor.cells(1,1).value=cellCurrent.value) Then

【讨论】:

    【解决方案2】:

    希望你正在寻找这个。

    Function CountCellsByColor(rData As Range, cellRefColor As Range, searchtext As String) As Long
        Dim indRefColor As Long
        Dim cellCurrent As Range
        Dim cntRes As Long
        Application.Volatile
        cntRes = 0
        indRefColor = cellRefColor.Cells(1, 1).Interior.Color
        For Each cellCurrent In rData
            If indRefColor = cellCurrent.Interior.Color And cellCurrent.Value = searchtext Then
                cntRes = cntRes + 1
            End If
        Next cellCurrent
        CountCellsByColor = cntRes
    End Function
    

    工作示例

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-17
      • 2018-04-24
      • 1970-01-01
      相关资源
      最近更新 更多