excel 如何访问一个类示例,当这个类在一个集合中,而这个集合又在一个类中,而这个类又在一个集合中?

new9mtju  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(88)

我有一个非常复杂的数据结构,但我想看看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

请注意,即使我没有显示所有变量的初始化,每个变量都是定义和可访问的。

mwkjh3gx

mwkjh3gx1#

在第一个代码中,index位于operations的元素数的前面。尝试实现这一点:

Public Sub Search()
  Dim i As Long, j As Long, number_program As Long, index As Long
  For i = 1 To Element.Count
    index = 0 ' suppose operations is empty
    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
  Next i
End Sub

相关问题