vba 多参数函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40441436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Function with multiple parameters
提问by spacedout
I have a VBA function with multiple arguments, which I need to get from the excel sheet and not from a sub, how do I divide the inputs into the number of parameters i need for my function?
我有一个带有多个参数的 VBA 函数,我需要从 Excel 表而不是从子表中获取它,如何将输入划分为我的函数所需的参数数量?
Ex:
前任:
Function func(a as double,b as double) as double
'calculations
'some return value
End Function
This is how I have been trying to get the values:
这就是我一直试图获取值的方式:
回答by user3598756
if you want to handle multiple arguments of which you don't know the number of, then use ParamArray
argument
如果你想处理多个你不知道数量的ParamArray
参数,那么使用参数
for instance, assuming func()
should simply sum the arguments you pass it:
例如,假设func()
应该简单地对传递给它的参数求和:
Function func(ParamArray args() As Variant) As Double
Dim i As Long
Dim cell As Range
For i = LBound(args) To UBound(args) '<--| loop through each passed argument
If TypeName(args(i)) = "Range" Then '<--| if the current element is a Range
For Each cell In args(i) '<--| loop through range cells
func = func + cell.Value
Next cell
Else '<--| otherwise
func = func + args(i) '<--| simply process the current argument value
End If
Next i
End Function
回答by Steven Block
Is this what you want?
这是你想要的吗?
Code:
代码:
Function TestFunction(a As Double, b As Double) As Double
TestFunction = a + b
End Function
Excel Sheet:
Excel表格:
=TestFunction(A1,B1)