简体   繁体   English

excel COUNTIF 使用单元格背景选项计算单词

[英]excel COUNTIF Count Words with a Cell Background Option

Is it possible to use the CountIf function with advanced options: count cells containing a specific string only if the cell background is of a specific color .是否可以将 CountIf 函数与高级选项一起使用:仅当单元格背景为特定颜色时才计算包含特定字符串的单元格。

I'm using the Excel formula: `=COUNTIF(page001!B:B;" id-p01 "), but blocks of data on each sheet have unique strings, each block could have two different background colors: GREEN or BLUE.我正在使用 Excel 公式:`=COUNTIF(page001!B:B;" id-p01 "),但是每个工作表上的数据块都有唯一的字符串,每个块可以有两种不同的背景颜色:绿色或蓝色。 So what i'm asking is if i can get a function which would eg COUNT cells containing "id-p01" on a selected sheet, but ONLY those with a GREEN background color.所以我要问的是我是否可以得到一个函数,例如在选定的工作表上计算包含“id-p01”的单元格,但只有那些具有绿色背景颜色的单元格。

Here is an example of how the sheet looks like:以下是工作表外观的示例:

=COUNTIF(page001!B:B;"*id-p01*")

With this formula: =COUNTIF(page001!B:B;"*id-p01*") It counts id-p01 on the selected sheet in the B:B column.使用此公式: =COUNTIF(page001!B:B;"*id-p01*")它计算B:B列中所选工作表上的id-p01

Is it possible to make it count only GREEN background colored cells?是否有可能使它计算绿色的背景颜色的细胞?

This quick solution will print out on the screen the number of cells within the Range B1 to B1000 (you can modify the Range if you've more/less rows to test) that have exactly your green color.这个快速解决方案将在屏幕上打印出范围 B1 到 B1000 内的单元格数量(如果您有更多/更少的行要测试,您可以修改范围)完全具有您的绿色。

Note that you have to use a macro to do this, it can't be achieved with a simple formula.请注意,您必须使用宏来执行此操作,无法通过简单的公式来实现。 To create a macro, press ALT + F11, then right-click on your Workbook's name and "Insert Module".要创建宏,请按 ALT + F11,然后右键单击工作簿的名称和“插入模块”。 Copy paste the code below and press F5 while you're still in the VBA window or use any other method to run the macro.复制粘贴下面的代码并在您仍在 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

Let me know if this helped.如果这有帮助,请告诉我。

Eleove埃莱夫

Count If Value And Color计数如果值和颜色

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

That's nice, but what's the 'Interior ColorIndex' of the color in this cell?很好,但是此单元格中颜色的“内部颜色索引”是多少?

Cell Interior Color Index细胞内部颜色指数

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM