所以我一直在尝试创建一个在VBA中添加分贝的功能。我试过使用ParamArray,但我在处理信息方面遇到了麻烦。理想情况下,它将作为SUM()工作,只需将所有输入放入公式和繁荣中。
现在,我有:
Function DBADD3(ParamArray nums()) As Double
Dim DBPrTot As Variant
'this will be input into the log function at the end
DBPrTot = 0
'initializing value for use in for loop
For i = LBound(nums) To UBound(nums)
DBPrTot = DBPrTot + 10 ^ (nums(i) / 10)
'all of the values gathered from ParamArray are being input into this running total
Next i
DBADD3 = 10 * WorksheetFunction.Log10(DBPrTot)
'throwing the DBPrTot running value into our end equation
End Function
我希望像DBADD3(A1:A3,A5,A7)那样输入并仍然产生一些东西。有人可以帮忙吗?
答案 0 :(得分:4)
因此,您需要了解传入的信息类型。在您之前的另一篇文章中,数字是直接传递的,因此可以轻松访问它们的值。在这种情况下,您传入一系列范围(范围和单个单元格组)。因此,您必须遍历每个数组以拉出范围组,然后遍历范围以获取单个值。
请参阅下面的基本SUM
样式函数示例:
Function ReturnValues(ParamArray args()) As Double
Dim dRunningTotal As Double
dRunningTotal = 0
For i = LBound(args) To UBound(args)
For Each cel In args(i)
dRunningTotal = dRunningTotal + cel.Value
Next cel
Next i
ReturnValues = dRunningTotal
End Function
已编辑添加:
您可以测试参数的类型,以便防止抛出错误,还可以处理参数中可能包含范围和值的情况,即=ReturnValues(A1:A10, 7, 9, B1:B4)
Function ReturnValues(ParamArray args()) As Double
Dim dRunningTotal As Double
dRunningTotal = 0
For i = LBound(args) To UBound(args)
If TypeName(args(i)) = "Range" Then
For Each cel In args(i)
dRunningTotal = dRunningTotal + cel.Value
Next cel
Else
dRunningTotal = dRunningTotal + args(i)
End If
Next i
ReturnValues = dRunningTotal
End Function