对单元格的颜色进行计算只能对列进行求和吗?
来源:百度知道 编辑:UC知道 时间:2024/06/27 10:49:03
我做了一个考勤表,里面用颜色标示了他们上的是白、夜、中班的区别,现在我想用公式反应出他们当月的白、夜、中班天数。
用VBA,新建一个宏,输入代码如下:
Function 背景颜色求和(Color As Range, rng As Range)
Dim cell As Range, iCol As Integer, andy, sky As Integer
Application.Volatile
iCol = Color.Interior.ColorIndex
For Each cell In rng
If cell.Interior.ColorIndex = iCol Then
sky = sky + 1
andy = WorksheetFunction.Sum(cell) + andy
End If
Next cell
If sky = 0 Then MsgBox "区域中无此背景色", vbDefaultButton1 + 64, "andysky提示"
背景颜色求和 = andy
End Function
然后你就可以用“背景颜色求和()”这个函数了,使用方法如同“SUM()”等函数。
如:“=背景颜色计数(A1,A1:B10)”
A1 代表取色单元格 任意,只要有颜色就行
A1:B10 代表统计值的范围
PS:只对背景色有效哦,对字体颜色无效,
要有效的话,把Interior替换成font
鸣谢:百宝箱4.0
应该要用到vb吧
可参考
http://zhidao.baidu.com/question/1334920.html