迭代Collection时,对象需要错误'424'

时间:2015-05-20 16:21:32

标签: excel vba excel-vba excel-2010 excel-2007

当我运行我的功能时,我收到以下错误:

Run-Time error '424':  Object Required

它突出显示以下代码行:

For Each n In DataSet

这是我的完整代码:

'==============================|Find_Occurrences FUNCTION|=============================='
' Given a collection and a search value, return the number of occurrences of the search
' value in the collection, as an Integer.  If it does not exist, return the integer 0
Function Find_Occurrences(DataSet As Collection, What As Variant) As Integer

    Dim Count As Integer
    Count = 0
    Dim n As Variant
    For Each n In DataSet ''''''''''''''''''Object required error'''''''''''''''''
        'MsgBox (TypeName(What))
        If (TypeName(n) = TypeName(What)) And (n = What) Then
            Count = Count + 1
        End If
    Next n
    Find_Occurrences = Count

End Function

是否有人知道可能导致此问题的原因,无论是在我的代码中还是在我的代码之外?

1 个答案:

答案 0 :(得分:0)

当调用函数时,错误实际上在函数之外。它不会传入DataSet的集合,而是传递任何东西。