Excel 2003 没有公式可以按颜色加总,计数。
以下是vba自定函数,可以求加总,计数,平均数。
将以下vba载入 "模块" ,跟著关掉vba编辑窗口。
Private Function bycol(target As Range, sample As Range, xtype As String)
Dim cell As Range, xsum, xcnt
For Each cell In target
If cell.Interior.ColorIndex <> sample(1).Interior.ColorIndex Then GoTo 888
If IsEmpty(cell) Then GoTo 888
If VarType(cell) <> 5 Then GoTo 888
xsum = xsum + cell.Value
xcnt = xcnt + 1
888:
Next
If xtype Like "[Ss]" Then
bycol = xsum
ElseIf xtype Like "[Cc]" Then
bycol = xcnt
ElseIf xtype Like "[Aa]" Then
bycol = Application.Evaluate("=" & xsum & "/" & xcnt)
Else
bycol = "#错误"
End If
End Function
公式原型:
=bycol(统计区域,样板格,类形)
此例:
加总
=bycol(A1:D6,B5,"s")
计数
=bycol(A1:D6,B5,"c")
平均
=bycol(A1:D6,B5,"a")
以下是vba自定函数,可以求加总,计数,平均数。
将以下vba载入 "模块" ,跟著关掉vba编辑窗口。
Private Function bycol(target As Range, sample As Range, xtype As String)
Dim cell As Range, xsum, xcnt
For Each cell In target
If cell.Interior.ColorIndex <> sample(1).Interior.ColorIndex Then GoTo 888
If IsEmpty(cell) Then GoTo 888
If VarType(cell) <> 5 Then GoTo 888
xsum = xsum + cell.Value
xcnt = xcnt + 1
888:
Next
If xtype Like "[Ss]" Then
bycol = xsum
ElseIf xtype Like "[Cc]" Then
bycol = xcnt
ElseIf xtype Like "[Aa]" Then
bycol = Application.Evaluate("=" & xsum & "/" & xcnt)
Else
bycol = "#错误"
End If
End Function
公式原型:
=bycol(统计区域,样板格,类形)
此例:
加总
=bycol(A1:D6,B5,"s")
计数
=bycol(A1:D6,B5,"c")
平均
=bycol(A1:D6,B5,"a")