【发布时间】:2020-07-29 02:00:43
【问题描述】:
我正在尝试做一个简单的平均函数来做一些额外的数据处理。
我面临的问题是我当前可以使用我的代码 @987654321 @进行范围输入,但是选择多个输入([[在单元格中] @987654322 @时,它都会破裂。
。所以我决定不做一个通用的函数代码,而是单独写一个代码来处理问题
Public Function AvgFunc_Selection(arg1 as Double, arg2 as Double, arg3 as Double, arg4 as Double, arg5 as Double)
但是我显然做错了什么,因为我无法超越这一点。
关于如何制作一个适用于多个输入的函数有什么建议吗?
这里是完整的代码:
Public Function AvgFunc_Selection(arg1 As Double, arg2 As Double, arg3 As Double, arg4 As Double, arg5 As Double)
Dim temp1 As Double
Dim temp2 As Double
Dim Count As Integer
temp1 = arg1
If temp1 > 0 Then
temp2 = temp2 + temp1
Count = Count + 1
End If
temp1 = arg2
If temp1 > 0 Then
temp2 = temp2 + temp1
Count = Count + 1
End If
temp1 = arg3
If temp1 > 0 Then
temp2 = temp2 + temp1
Count = Count + 1
End If
temp1 = arg4
If temp1 > 0 Then
temp2 = temp2 + temp1
Count = Count + 1
End If
temp1 = arg5
If temp1 > 0 Then
temp2 = temp2 + temp1
Count = Count + 1
End If
AvgFunc_Selection = temp2 / Count
End Function
【问题讨论】:
-
你的函数的邮政编码。
-
你的输入、输出和计算是什么?
-
添加了完整的代码。是的,它看起来像一个简单的 AverageIf 函数,但它是大型项目的基础。
-
传递给它一个数组而不是单个变量,然后例程可以检查数组的边界。
-
我不能必须将单个变量传递给它。我选择的 excel 变量总是在变化,我的函数必须适应这些变化。我当前的代码使用数组,我需要找到一种方法来处理没有数组的多个变量。我希望 excel 能显示 WorksheetFunction.average() 函数背后的代码(=average() 在 excel 单元格中)