我有一个非常复杂的数据结构,但我想看看vba如何对此作出React。
Public Sub Search()
Dim i As Long, j As Long, number_program As Long, index As Long: index = 1
For i = 1 To Element.Count
While //something
If //something
Dim new_operation As Class3
Set new_operation = New Class3
index = index + 1
new_operation.value1 = 1
new_operation.value2 = 2
Element(i).operations.Add new_operation
End If
//something giving a value to number_program
If number_program > 0 Then
Element(i).operations(index).value3 = 3
End If
Wend
index = 1
Next i
End Sub
所以这里Element(i)
是一个类,它在名为Element
的集合中。对象Element(i).operations
是一个集合,在这个集合中,我将有一个未定义数量的类Element(i).operations(j)
。
我得到了错误:执行“9”时出错:索引不属于该行上的选择:Element(i).operations(index).value3 = 3
在我的代码中,我有这样一行:Articles_triés(i).operations(j).Count
也会出现错误
唯一可能出错的是我的集合在Element(i)
类中的声明:
Private m_operations As Collection
Public Property Get operations_article() As Collection
If m_operations Is Nothing Then
Set m_operations = New Collection
End If
Set operations = m_operations
End Property
Private Sub Class_Initialize()
Set m_operations = New Collection
End Sub
请注意,即使我没有显示所有变量的初始化,每个变量都是定义和可访问的。
1条答案
按热度按时间mwkjh3gx1#
在第一个代码中,
index
位于operations
的元素数的前面。尝试实现这一点: