【问题标题】:excel COUNTIF Count Words with a Cell Background Optionexcel COUNTIF 计算带有单元格背景选项的单词
【发布时间】:2018-10-28 22:09:37
【问题描述】:

是否可以将 CountIf 函数与高级选项一起使用:仅当单元格背景为特定 color 时,才计算包含特定 string 的单元格。

我正在使用 Excel 公式:`=COUNTIF(page001!B:B;"id-p01"),但每张工作表上的数据块都有唯一的字符串,每个块可以有两种不同的背景颜色:绿色或蓝色。所以我要问的是我是否可以获得一个功能,例如所选工作表上包含“id-p01”的 COUNT 个单元格,但仅限于具有绿色背景颜色的单元格。

以下是工作表外观示例:

使用这个公式:=COUNTIF(page001!B:B;"*id-p01*") 它在B:B 列中的选定工作表上计算id-p01

是否可以使其计数绿色背景颜色的单元格?

【问题讨论】:

标签: excel vba excel-formula user-defined-functions countif


【解决方案1】:

此快速解决方案将在屏幕上打印出 B1 到 B1000 范围内的单元格数量(如果要测试的行更多/更少,您可以修改范围),这些单元格的颜色与您的绿色完全相同。

请注意,您必须使用宏来执行此操作,这无法通过简单的公式来实现。 要创建宏,请按 ALT + F11,然后右键单击您的工作簿名称和“插入模块”。复制粘贴下面的代码并在您仍在 VBA 窗口中时按 F5 或使用任何其他方法运行宏。

Sub CountWithColor()

For Each c In Range("B1:B1000")
    If c.Value Like "*id-p01*" And c.Interior.Color = RGB(226, 239, 218) Then
    compteur = compteur + 1
    End If
Next c

MsgBox (compteur)

End Sub

如果这有帮助,请告诉我。

埃洛夫

【讨论】:

    【解决方案2】:

    如果值和颜色计数

    Function CIVAC(Range As Range, Value As Variant, _
        Optional ColorIndex As Long = -4142, _
        Optional Compare As Integer = 1) As Long
    'Title
      'Count If Value And Color
    'Description
      'In a specified contiguous range, counts the number of cells both,
      'containing a specified value and having a specified Interior ColorIndex.
    
      Dim arrVal As Variant 'Range Array
      Dim arrClr() As Long 'ColorIndex Array
      Dim lngVal As Long 'Row Counter
      Dim iVal As Integer 'Column Counter
      Dim lngResult As Long 'Result Accumulator
    
      'Values
      arrVal = Range.Areas(1) 'Prevent Multiple Areas Error
    
      'ColorIndexes
      ReDim arrClr(LBound(arrVal) To UBound(arrVal), _
          LBound(arrVal, 2) To UBound(arrVal, 2))
      For lngVal = LBound(arrClr) To UBound(arrClr)
        For iVal = LBound(arrClr, 2) To UBound(arrClr, 2)
          arrClr(lngVal, iVal) = Range.Cells(lngVal, iVal).Interior.ColorIndex
        Next
      Next
    
      'Count
      For lngVal = LBound(arrClr) To UBound(arrClr)
        For iVal = LBound(arrClr, 2) To UBound(arrClr, 2)
          If Not IsError(arrVal(lngVal, iVal)) Then 'Prevent VBA Errors
            If InStr(1, arrVal(lngVal, iVal), Value, Compare) <> 0 And _
                arrClr(lngVal, iVal) = ColorIndex Then lngResult = lngResult + 1
          End If
        Next
      Next
    
      CIVAC = lngResult
    
    End Function
    

    很好,但是这个单元格中颜色的“内部颜色索引”是多少?

    细胞内部颜色指数

    Function CICI(CellRange As Range) As Long
    'Title
      'Cell Interior Color Index
    'Description
      'Returns the Interior ColorIndex of a specified cell ('CellRange').
      'If 'CellRange' contains more than one cell, it uses the first cell.
    
      CICI = CellRange(1, 1).Interior.ColorIndex
    
    End Function
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-13
      相关资源
      最近更新 更多