【自定函数‧按颜色加总,及计数】
excel2007吧
全部回复
仅看楼主
level 5
siaojuen 楼主
将以下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")
2012年04月07日 07点04分 1
1