0

我正在构建和引用自定义对象的集合。该类名为 CProject,具有 Name 和 RefNum 两个属性。

创建和引用的代码如下所示:

Dim Projects As Collection

Sub BuildProjects()
      Dim i As Integer
      Set Projects = New Collection

  For Each c In Worksheets("Active Projects").Range("A4:A750").Cells
    If IsEmpty(c) Then

        'Do some stuff

    Else
        If Projects.Count > 0 Then
           For i = 1 To Projects.Count
                If c.Value = Projects.Item(i).Name Then '<---Error Occurs Here

                    'Do some Duplicate Management Stuff

                End If
            Next i
        End If
    End If
Next
End Sub

当我运行此代码时,我收到错误 424:需要对象。我相信我提供了一个合格的对象标识符。有人看到我在哪里犯了错误吗?

4

1 回答 1

0

Collection 项目是 Variant 类型,因此您可以使用以下内容进行更改:

Dim Projects As Collection

Sub BuildProjects()
      Dim i As Integer
      dim var_item as variant
      dim aCProject as CProject

      Set Projects = New Collection

  For Each c In Worksheets("Active Projects").Range("A4:A750").Cells
    If IsEmpty(c) Then

        'Do some stuff

    Else
        For Each var_item in Projects
            set aCProject = var_item
            If c.Value = aCProject.Name Then 

                'Do some Duplicate Management Stuff

            End If
        Next 
    End If
Next
End Sub
于 2013-11-26T14:58:03.253 回答