将数组传递给集合

时间:2017-07-05 16:07:07

标签: vba excel-vba excel

我正在尝试将数组传递给集合,但我似乎无法填充该集合。

Public Function CreateCol(ws As Worksheet, ary, col As collection)
Dim rng As Range, collect As collection
Dim y, skey, svalue

    On Error Resume Next
    'populate fund list

    For y = LBound(ary) To UBound(ary)
        If ary(y) <> "" Then
            skey = Trim(ary(y))
            svalue = WorksheetFunction.SumIf(ws.Range("A:A"), ary(y), ws.Range("P:P"))
            collect.Add svalue, skey
        End If
    Next y


End Function

1 个答案:

答案 0 :(得分:0)

正如Mat的Mug所说,确保将集合设置为对象引用。一种方法是将set collect = new collection放在for循环之前。