Vba 访问项集合中的属性

Vba 访问项集合中的属性,vba,excel,excel-2010,Vba,Excel,Excel 2010,我有一系列的课程。但我似乎无法访问类的属性。这是我能做的吗 这是我的clsProj课程: Option Explicit Private pValue As String Public Property Get Value() As String Value = pValue End Property Public Property Let Value(tempv As String) pValue = tempv End Property 我的下属: Sub testt

我有一系列的课程。但我似乎无法访问类的属性。这是我能做的吗

这是我的clsProj课程:

Option Explicit
Private pValue As String


Public Property Get Value() As String
    Value = pValue
End Property

Public Property Let Value(tempv As String)
    pValue = tempv
End Property
我的下属:

Sub testtt()

Set cp = New Collection

cp.Add clsProj, "AAA"
cp.Add clsProj, "BBB"

cp("AAA").Value = "OK"

MsgBox (cp("AAA").Value)

End Sub

总之,我有一个类的集合clsProj,我用字符串对其进行索引(这只是一个测试子项),在本例中,我希望访问给定集合项ex:AAA的clsProj属性。这里哪里不对?我似乎不明白。类有点难理解,但当你理解它们时,它们真的很有用。也许这会有点帮助:

Sub testtt()

    Dim cp As Collection
    Set cp = New Collection

    Dim blabla As clsProj
    Set blabla = New clsProj

    Dim blabli As clsProj
    Set blabli = New clsProj

    blabla.Value = "OK"
    blabli.Value = "KO"

    cp.Add blabla, "AAA"
    cp.Add blabli, "BBB"

    MsgBox (cp("AAA").Value)
    MsgBox (cp("BBB").Value)

    Set blabla = Nothing
    Set blabli = Nothing

End Sub

编辑:混合
集合
用于…下一步
循环:

Sub testtt()

    Dim cp As Collection
    Set cp = New Collection

    Dim blabla As clsProj
    Dim i As Integer
    For i = 1 To 10
        Set blabla = New clsProj

        '"OK" value + a special character from ASCII table
        blabla.Value = "OK " & Chr(32 + i)

        cp.Add blabla, CStr("AAA" & i)

        Set blabla = Nothing
    Next i

    'Test calling collection by key
    MsgBox cp("AAA5").Value

    'Test calling collection by item number and print it in
    '"Immediate" window (ctrl+g to show that window from VBA editor)
    For i = 1 To cp.Count
        Debug.Print cp(i).Value
    Next i

End Sub

这正是我想要做的!Thanks@user2385809; 很高兴我能帮忙!我又添加了一个示例,使用
For…Next
循环:)