mrd*_*iri 13 excel vba excel-vba
我正在获取一组单元格,并在下面的函数中对它们进行一些计算.
如果我将一个范围(带:符号)作为第一个参数传递,它会起作用,但如果我选择一些单元格作为其范围(A1, A3, B6, B9),则它会失败.它只是在逗号之前获得第一个单元格作为第一个参数.但我想要整个细胞.
我能做什么?(除了使用字符串传递范围)
Function calculateIt(Sessions As Range, Customers As Range) As Single
' calculate them...
End Function
Run Code Online (Sandbox Code Playgroud)
还有一件事:是否可以将一组范围作为参数传递?怎么样?
chu*_*uff 24
如上所述,您的函数只接受两个范围作为参数.
要允许在函数中使用可变数量的范围,您需要在参数列表中声明ParamArray变量数组.然后,您可以依次处理数组中的每个范围.
例如,
Function myAdd(Arg1 As Range, ParamArray Args2() As Variant) As Double
Dim elem As Variant
Dim i As Long
For Each elem In Arg1
myAdd = myAdd + elem.Value
Next elem
For i = LBound(Args2) To UBound(Args2)
For Each elem In Args2(i)
myAdd = myAdd + elem.Value
Next elem
Next i
End Function
Run Code Online (Sandbox Code Playgroud)
然后可以在工作表中使用此函数来添加多个范围.

对于您的功能,存在可以传递给函数的哪个范围(或单元格)是"会话"以及哪些是"客户"的问题.
最简单的处理方法是,如果您确定第一个范围是Sessions,则后续范围是Customers.
Function calculateIt(Sessions As Range, ParamArray Customers() As Variant) As Double
'This function accepts a single Sessions range and one or more Customers
'ranges
Dim i As Long
Dim sessElem As Variant
Dim custElem As Variant
For Each sessElem In Sessions
'do something with sessElem.Value, the value of each
'cell in the single range Sessions
Debug.Print "sessElem: " & sessElem.Value
Next sessElem
'loop through each of the one or more ranges in Customers()
For i = LBound(Customers) To UBound(Customers)
'loop through the cells in the range Customers(i)
For Each custElem In Customers(i)
'do something with custElem.Value, the value of
'each cell in the range Customers(i)
Debug.Print "custElem: " & custElem.Value
Next custElem
Next i
End Function
Run Code Online (Sandbox Code Playgroud)
如果要包含任意数量的Sessions范围和任意数量的Customers范围,则必须包含一个参数,该参数将告诉函数,以便它可以将Sessions范围与Customers范围分开.
此参数可以设置为函数的第一个数字参数,该参数将标识以下参数中有多少是Sessions范围,其余参数隐式为Customers范围.该函数的签名将是:
Function calculateIt(numOfSessionRanges, ParamAray Args() As Variant)
Run Code Online (Sandbox Code Playgroud)
或者它可以是一个"保护"参数,将Sessions范围与Customers范围分开.然后,您的代码必须测试每个参数以查看它是否是警卫.该功能看起来像:
Function calculateIt(ParamArray Args() As Variant)
Run Code Online (Sandbox Code Playgroud)
也许通过电话之类的话:
calculateIt(sessRange1,sessRange2,...,"|",custRange1,custRange2,...)
Run Code Online (Sandbox Code Playgroud)
程序逻辑可能是这样的:
Function calculateIt(ParamArray Args() As Variant) As Double
...
'loop through Args
IsSessionArg = True
For i = lbound(Args) to UBound(Args)
'only need to check for the type of the argument
If TypeName(Args(i)) = "String" Then
IsSessionArg = False
ElseIf IsSessionArg Then
'process Args(i) as Session range
Else
'process Args(i) as Customer range
End if
Next i
calculateIt = <somevalue>
End Function
Run Code Online (Sandbox Code Playgroud)
还有另一种方法可以将多个范围传递给一个函数,我认为这对用户来说感觉更清晰.在电子表格中调用函数时,将每组范围包装在括号中,例如:calculateIt( (A1,A3), (B6,B9) )
上面的调用假定您的两个会话在A1和A3中,而您的两个客户在B6和B9中.
要使其工作,您的函数需要遍历Areas输入范围中的每一个.例如:
Function calculateIt(Sessions As Range, Customers As Range) As Single
' check we passed the same number of areas
If (Sessions.Areas.Count <> Customers.Areas.Count) Then
calculateIt = CVErr(xlErrNA)
Exit Function
End If
Dim mySession, myCustomers As Range
' run through each area and calculate
For a = 1 To Sessions.Areas.Count
Set mySession = Sessions.Areas(a)
Set myCustomers = Customers.Areas(a)
' calculate them...
Next a
End Function
Run Code Online (Sandbox Code Playgroud)
好消息是,如果你将两个输入作为一个连续的范围,你可以像正常的那样调用这个函数,例如calculateIt(A1:A3, B6:B9).
希望有帮助:)
| 归档时间: |
|
| 查看次数: |
82470 次 |
| 最近记录: |