计算具有相同背景颜色的单元格列表
每个单元格包含一些文本和背景颜色。 所以我有一些蓝色和一些红色的细胞。 我用什么函数来计算红细胞的数量?
我试过了=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)
如果单元格的颜色格式为负值(否则返回0
),则工作表公式=CELL("color",D3)
将返回1
。
你可以用一点VBA解决这个问题。 将其插入到VBA代码模块中:
Function CellColor(xlRange As Excel.Range) CellColor = xlRange.Cells(1, 1).Interior.ColorIndex End Function
然后使用函数=CellColor(D3)
显示D3
的.ColorIndex
我刚刚创build这个,它看起来更容易。 你得到这两个function:
=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 )
在模块中创build这2个VBA函数(按Alt-F11)
打开+文件夹。 双击Module1
只需粘贴下面的文本,然后closures模块窗口(它必须保存它):
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
我需要解决完全相同的任务。 我用不同的背景颜色对不同的部分进行了视觉上的分割。 使用Googlesearch我已经find这个页面https://support.microsoft.com/kb/2815384 。 不幸的是,它不能解决问题,因为ColorIndex指的是一些不可预知的值,所以如果某些单元格有一种颜色的细微差别(例如颜色亮度的不同值),则build议的函数会对它们进行计数。 下面的解决scheme是我的修复:
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
是的VBA是要走的路。
但是,如果您不需要使用公式自动计算/更新具有特定颜色的单元格数量的单元格,则可以使用“查找和replace”function并将单元格格式化为适当的颜色填充。
点击“查找全部”将会显示对话框左下方的单元格总数。
如果您的search范围很大,这变得特别有用。 VBA脚本将非常缓慢,但“查找和replace”function仍然非常快。