从集合中删除对象-类型不匹配的VBA

时间:2019-04-22 00:05:54

标签: excel vba

我正在尝试使用带有VBA的Excel中的集合从计划中添加和删除人员。步骤是:

  1. 创建100个人,并在计划的第一周分配他们
  2. 从第一周起将其删除,然后计算下一周
  3. 将该人添加到下一个预定的星期。

第二个循环提供程序的删除部分出现运行时错误13,类型不匹配错误,类似于this问题。我已经在上一个问题中尝试过使用相同错误的解决方案。我不确定如何引用person索引号来删除适当的person对象。

   Sub test()

    Dim person as New Clsperson 'User defined Object
    Dim i as Integer
    Dim j as Integer
    Dim next_list as Integer
    Dim weeks(1 To 52) as New Collection

    'Creating 100 people, assigning them first week, and placing on collection
    For i = 1 To 100
        Set person = New Clsperson
        person.first_time =  WorksheetFunction.RandBetween(1, 5)
        weeks(person.first_time).add person  
    Next i

     For i = 1 To 52
        If weeks(i).Count > 0 Then
            For j = 1 To weeks(i).Count
                Set person= weeks(i).Item(j)

                weeks(i).Remove person 'THIS PROVIDES ERROR

                next_list = WorksheetFunction.RandBetween(4, 6)

                person.next_time = person.first_time+ next_list 

                If person.next_time> 52 Then
                    person.next_time = person.next_time - 52
                End If

            weeks(person.next_time).add person

            Next j
        End If
    Next i
End Sub

0 个答案:

没有答案