|
有版友问能否如同Excel一样横向的求字段的平均值(其中如果字段值为空这不在平均值计算范围内),对于这个问题需要用自定义函数解决。在自定义函数中,要用到可变参数概念,也就是说需要适应带入的参数个数变化。这一方法是在事先无法确定参数数量情况下常用的一种方法。
- Function DDMax(ParamArray A() As Variant) As Single
- '示例:select *,DDmax(字段1,字段2,字段3,字段4) as 最大值 from 表1
- Dim i As Long
- Dim Mx As Single
- Mx = -1 * 10 ^ 10
- For i = 0 To UBound(A, 1)
- If IsNumeric(A(i)) = True Then
- If A(i) > Mx Then Mx = A(i)
- End If
- Next
- DDMax = Mx
- End Function
- Function DDMin(ParamArray A() As Variant) As Single
- '示例:select *,DDmin(字段1,字段2,字段3,字段4) as 最小值 from 表1
- Dim i As Long
- Dim Mn As Single
- Mn = 10 ^ 10
- For i = 0 To UBound(A, 1)
- If IsNumeric(A(i)) = True Then
- If A(i) < Mx Then Mx = A(i)
- End If
- Next
- DDMin = Mn
- End Function
- Function DDAvg(ParamArray A() As Variant) As Single
- '示例:select *,DDAvg(字段1,字段2,字段3,字段4) as 平均值 from 表1
- Dim i As Long
- Dim S As Single
- Dim C As Long
- S = 0: C = 0
- For i = 0 To UBound(A, 1)
- If IsNumeric(A(i)) = True Then
- S = S + A(i)
- C = C + 1
- End If
- Next
- If C <> 0 Then
- DDAvg = S / C
- Else
- DDAvg = 0
- End If
- End Function
复制代码
|
|