2016-11-05 138 views
-2

我有一个带有多个参数的VBA函数,我需要从Excel表格中获取,而不是从一个子表格获取,我如何将输入分成我需要的参数数量?具有多个参数的功能

例:

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

回答

0

这是你想要的吗?

代码:

Function TestFunction(a As Double, b As Double) As Double 

    TestFunction = a + b 

End Function 

Excel工作表:

=TestFunction(A1,B1) 
+0

是的,但是,当我选择excel中的单元格,并用逗号分隔参数时,仍然出现错误。我希望函数所在的单元格表示该值是错误的。我想知道的是,如何手动输入三个Excel单元格值作为vba函数的参数。 – spacedout

+0

我对逗号和分歧的区别的理解是,某些语言设置使用一个,而其他语言使用另一个。对于你遇到的问题,我需要知道你正在使用的方程,然而,单元有时被格式化为文本或其他格式。另一件有时候会出错的事情是零分情况。你用调试器完成了计算吗? –

2
如果要处理多个参数,其中你不知道的号码,然后使用 ParamArray参数

例如,假设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