每个单元格包含一些文本和背景颜色。所以我有一些蓝色的细胞和一些红色的细胞。我用什么函数来计算红细胞的数量?
我已尝试=COUNTIF(D3:D9,CELL("color",D3))
但没有成功(D3
为红色)。
答案 0 :(得分:9)
Excel无法使用内置函数收集该属性。如果您愿意使用某些VB,那么所有与颜色相关的问题都会在这里得到解答:
http://www.cpearson.com/excel/colors.aspx
网站示例:
SumColor函数是基于颜色的 SUM和SUMIF的类比 功能。它允许您指定 范围的单独范围 颜色索引要检查和 值为的单元格范围 总结一下。如果这两个范围是 同样,该函数对单元格求和 其颜色与指定的颜色匹配 值。例如,以下内容 公式对B11:B17中的值求和 填充颜色为红色。
=SUMCOLOR(B11:B17,B11:B17,3,FALSE)
答案 1 :(得分:6)
如果单元格的格式为负值,则工作表公式=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
的{{1}}
答案 2 :(得分:4)
我刚创建了它,看起来更容易。你有这两个功能:
=GetColorIndex(E5) <- returns color number for the cell
来自(小区)
=CountColorIndexInRange(C7:C24,14) <- returns count of cells C7:C24 with color 14
from(细胞范围,你想要计算的颜色数)
示例显示了颜色为14的细胞百分比
=ROUND(CountColorIndexInRange(C7:C24,14)/18, 4 )
在模块中创建这两个VBA函数(按Alt-F11)
打开+文件夹。双击Module1
只需将此文本粘贴到下面,然后关闭模块窗口(它必须保存):
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
答案 3 :(得分:2)
我需要完全解决同样的任务。我使用不同的背景颜色对表格进行了视觉划分。谷歌搜索互联网我找到了这个页面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
答案 4 :(得分:0)