每个单元格都包含一些文本和背景颜色。所以我有一些蓝色的细胞和一些红色的细胞。我用什么函数来计算红细胞的数量?
我试过=COUNTIF(D3:D9,CELL("color",D3))
没有成功(D3
红色在哪里)。
Excel 无法通过其内置函数收集该属性。如果您愿意使用一些 VB,所有与颜色相关的问题都在这里得到解答:
http://www.cpearson.com/excel/colors.aspx
网站示例:
SumColor 函数是 SUM 和 SUMIF 函数的基于颜色的模拟。它允许您为要检查其颜色索引的范围和要对其值求和的单元格范围指定单独的范围。如果这两个范围相同,则函数将颜色与指定值匹配的单元格相加。例如,以下公式对 B11:B17 中填充颜色为红色的值求和。
=SUMCOLOR(B11:B17,B11:B17,3,FALSE)
如果单元格的格式为负值的颜色,则工作表公式=CELL("color",D3)
返回(否则返回)。1
0
你可以用一点 VBA 来解决这个问题。将其插入 VBA 代码模块:
Function CellColor(xlRange As Excel.Range)
CellColor = xlRange.Cells(1, 1).Interior.ColorIndex
End Function
然后使用函数=CellColor(D3)
显示.ColorIndex
D3
我刚刚创建了这个,它看起来更容易。你得到这两个功能:
=GetColorIndex(E5) <- returns color number for the cell
从(单元格)
=CountColorIndexInRange(C7:C24,14) <- returns count of cells C7:C24 with color 14
从(单元格范围,要计算的颜色数)
示例显示颜色为 14 的单元格的百分比
=ROUND(CountColorIndexInRange(C7:C24,14)/18, 4 )
在模块中创建这 2 个 VBA 函数(按 Alt-F11)
打开 + 文件夹。双击模块 1
只需将此文本粘贴到下面,然后关闭模块窗口(然后必须保存它):
Function GetColorIndex(Cell As Range)
GetColorIndex = Cell.Interior.ColorIndex
End Function
Function CountColorIndexInRange(Rng As Range, TestColor As Long)
Dim cnt
Dim cl As Range
cnt = 0
For Each cl In Rng
If GetColorIndex(cl) = TestColor Then
Rem Debug.Print ">" & TestColor & "<"
cnt = cnt + 1
End If
Next
CountColorIndexInRange = cnt
End Function
我需要解决完全相同的任务。我为不同的部分使用不同的背景颜色在视觉上划分了表格。谷歌搜索互联网我发现了这个页面https://support.microsoft.com/kb/2815384。不幸的是,它不能解决问题,因为 ColorIndex 指的是一些不可预测的值,所以如果某些单元格具有一种颜色的细微差别(例如颜色的不同亮度值),建议的函数会计算它们。下面的解决方案是我的修复:
Function CountBgColor(range As range, criteria As range) As Long
Dim cell As range
Dim color As Long
color = criteria.Interior.color
For Each cell In range
If cell.Interior.color = color Then
CountBgColor = CountBgColor + 1
End If
Next cell
End Function