1、2007EXCEL使用技巧——按指定的单元格颜色和数字颜色进行计数或求和一、如果Excel工作表的某区域中包含不同的底纹颜色,我们可以用一个自定义函数对该区域按指定的单元格颜色进行计数或求和。方法是: 1.按Alt+F11,打开VBA编辑器。2.单击菜单“插入→模块”,将插入名称为“模块1”的模块在右侧的代码窗口中输入下列代码:1.SumByColor,可以对区域按指定单元格的颜色求和。Function SumByColor(Ref_color As Range, Sum_range As Range)Application.VolatileDim iCol A
2、s IntegerDim rCell As RangeiCol = Ref_color.Interior.ColorIndexFor Each rCell In Sum_rangeIf iCol = rCell.Interior.ColorIndex ThenSumByColor = SumByColor + rCell.ValueEnd IfNext rCellEnd Function2.CountByColor,可以统计区域中某种颜色的个数。Function CountByColor(Ref_color As Range, CountRange As Range)A
3、pplication.VolatileDim iCol As IntegerDim rCell As RangeiCol = Ref_color.Interior.ColorIndexFor Each rCell In CountRangeIf iCol = rCell.Interior.ColorIndex ThenCountByColor = CountByColor + 1End IfNext rCellEnd Function 上述两个自定义函数,一个是SumByColor,可以对区域按指定单元格的颜色求和。另一个是CountByColor,可以统计区域中
5、函数 我刚写了一个简单的VBA, 绝对可以做到你的要求 Function COLORSUM(xx As Range, yy As Range) As Doubley = yy.Font.ColorIndexFor Each x In xxIf x.Font.ColorIndex = y Thenxxx = xxx + x.ValueEnd IfNext COLORSUM = xxxEnd Function 假设你的数字是在A1:A100 ,而A2的数字颜色是作为自动求和的识别。只要将上述VBA贴在模块上, COLORSUM函数便可以使用 输入公式 =COLORSUM(