vba将一组单元格作为范围来运行

我得到一组单元格,并在下面的函数中对它们进行一些计算。

如果我将范围(以:符号)作为第一个parameter passing,但是如果我select一些单元格作为其范围( A1, A3, B6, B9 ),则失败。 它只是取得逗号前的第一个单元格作为第一个参数。 但是我想要整个细胞。

我能做什么? (除了使用string传递范围)

 Function calculateIt(Sessions As Range, Customers As Range) As Single ' calculate them... End Function 

还有一件事:是否可以传递一组范围作为参数? 怎么样?

正如所写,你的函数只接受两个范围作为参数。

为了允许在函数中使用可变数量的范围,需要在参数列表中声明一个ParamArrayvariables数组。 然后,您可以依次处理数组中的每个范围。

例如,

 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 

然后可以在工作表中使用该函数来添加多个范围。

myAdd使用

对于你的函数,可以传递给函数的范围(或单元格)是'会话'和'客户'。

最简单的情况下,如果你决定第一个范围是会话和任何后续的范围是客户。

 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 

如果要包含任意数量的“会话”范围和任意数量的“客户”范围,则必须包含一个参数,该参数将告诉该函数,以便将“会话”范围与“客户”范围分开。

该参数可以设置为函数的第一个数字参数,该参数将确定以下参数中有多less个为会话范围,其余参数隐式为Customers范围。 函数的签名将是:

 Function calculateIt(numOfSessionRanges, ParamAray Args() As Variant) 

或者它可能是一个“警卫”的说法,将客户范围与客户范围分开。 然后,你的代码将不得不testing每个参数,看看它是否是守卫。 该函数看起来像:

 Function calculateIt(ParamArray Args() As Variant) 

也许有一个像这样的电话:

 calculateIt(sessRange1,sessRange2,...,"|",custRange1,custRange2,...) 

程序的逻辑可能是这样的:

 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 

还有一种方法可以将多个范围传递给一个函数,我认为这对用户来说更加清晰。 当您在电子表格中调用函数时,可以将每组范围都包括在括号中,例如: calculateIt( (A1,A3), (B6,B9) )

上面的电话假设你的两个会话在A1和A3,而你的两个客户在B6和B9。

为了使这个工作,你的function需要循环通过input范围中的每个区域。 例如:

 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 

好的是,如果你的input是连续的,你可以像调用普通的那样调用这个函数,例如calculateIt(A1:A3, B6:B9)

希望帮助:)