EXCEL中自定义函数的语法是什么啊?

来源:百度知道 编辑:UC知道 时间:2024/06/05 20:35:23
那位高手能举例说明一下如何定义函数啊。要是有一两个例子更好,

Function colorsum(y As Range, rng, z As Integer)
Application.Volatile
Dim c As Double
Dim x As Range
colorsum = 0
For Each x In rng
If x.Interior.ColorIndex = y.Interior.ColorIndex Then
c = x.Offset(0, z).Value
Else
c = 0
End If
colorsum = colorsum + c
Next x
End Function

'公式例子 =colorsum(F3,$A$2:$A$80,2)*1 F3为指定颜色单元,2是偏移的列数,公式后加*1是为了自动刷新

设置E1为红色,E2为蓝色,在F1中输入=colorsum($E1,$A$1:$A$100,0)*1, F2中输入=colorsum($E2,$A$1:$A$100,0)*1