我正在尝试使用带有VBA的Excel中的集合从计划中添加和删除人员。步骤是:
第二个循环提供程序的删除部分出现运行时错误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