VBA自定义函数:求非零平均值

Sandy posted @ Dec 17, 2013 08:59:14 PM in VBA with tags VBA Excel , 2901 阅读

 

Public Function aveifnot0(ParamArray canshu() As Variant) As Double
'本函数为求非零平均数,参数为零散的单元格,而非单元格区域。求连续的单元格区域的非零平均值可以使用
'Excel内置函数AVERAGEIF
'定义函数,数组参数需要用关键字ParamArray定义,类型也必须为Variant
    Dim sum As Double     '声明sum为double型数据类型,sum是要计算非零数的累计和
    Dim i, k As Integer   '声明i,k为单整形
    sum = 0               '给sum初始化为零
    k = 0                 '给k初始化为零,k是计算非零数的个数
    For i = LBound(canshu) To UBound(canshu)   '开始循环,LBound取数组的第一位,UBound取数组的最后一位
    If canshu(i).Value > 0 Then                '判断数组里某一个数的值是否大于零
        sum = sum + canshu(i).Value            '如果大于零,将其值累加
        k = k + 1                              '如果大于零,计数
    Else                                       '否则不执行任何事情
    End If                                     '结束条件判断
    Next i                                     '循环
    aveifnot0 = sum / k                        '循环完毕,返回非零平均值
End Function                                   '自定义函数结束
  • 无匹配
SS 说:
2014年2月15日 22:49

请问 average 不可以吗?
除了它不判断 是不是>0之外 ,

Avatar_small
Sandy 说:
2014年2月24日 20:36

@SS: 不可以。average无论是否大于零都计数做平均。

SS 说:
2014年2月24日 20:50

@Sandy: 是的, 我知道这点, 我是想请教, 您写的这个函数,和自带的avg函数, 是不是只有">0"这点不同?

AP SSC fa 3 Question 说:
2022年9月08日 08:01

Formative Assessment means not only an examination, it includes various aspects such as Examination in completed lessons, Reflections, Project work done on the allotted topic and Self also Prepared notes etc. AP SSC fa 3 Question Paper Candidates of Telugu Medium, English Medium & Urdu Medium of the AP State can download the AP 10th Class FA 4 Model Paper 2023 Pdf with answers for the regular exams conducted by the Directorate of Government Examinations, Andhra Pradesh.

baños cerca de mi 说:
2023年7月28日 04:33

Cuando viajamos, necesitamos encontrar baños, baños o baños públicos en la ubicación actual. El uso de Google Maps, Google Assistant, Alexa, iPhone Siri y otros servicios de búsqueda de ubicación lo ayudan a obtener fácilmente los baños y baños públicos más cercanos. baños cerca de mi Aquí hemos discutido varios servicios de búsqueda de ubicación que lo ayudan a encontrar los baños públicos más cercanos a su ubicación actual donde se encuentra.

pavzi.com 说:
2024年1月17日 05:03

Pavzi.com is a startup by passionate webmasters and bloggers who have a passion for providing engaging content that is accurate, interesting, and worthy to read. pavzi.com We are more like a web community where you can find different information, resources, and topics on day-to-day incidents or news. We provide you with the finest web content on every topic possible with the help of the editorial and content team.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter