' CDays
Private pZone As New Collection
Public Property Get Zone() As Collection
Set Zone = pZone
End Property
Public Property Let Zone(Value As Variant)
For Each element In Value
pZone.Add element
Next
End Property
和sub
' Main Sub
dy.Zone.item("Zone1")(5) = 0
其中dy是一个实例 "区域1"被设置为下面
中Item1的键然而,这并没有改变Item(5)中的值。为什么呢?
答案 0 :(得分:0)
创建将包装将添加到VBA.Collection
的项目的类。然后可以使用集合引用添加/更改这些项目。 HTH
Class Foo
Option Explicit
Private m_id As Integer
Private m_value As String
Public Property Get ID() As Variant
ID = m_id
End Property
Public Property Let ID(ByVal vNewValue As Variant)
m_id = vNewValue
End Property
Public Property Get Val() As Variant
Val = m_value
End Property
Public Property Let Val(ByVal vNewValue As Variant)
m_value = vNewValue
End Property
模块
Option Explicit
Sub test1()
Dim col As VBA.Collection
Set col = New VBA.Collection
Dim f As Foo
Set f = New Foo
f.ID = 1
f.Val = "AAA"
col.Add f, VBA.CStr(f.ID)
Debug.Print col(f.ID).Val
col(f.ID).Val = "BBB"
Debug.Print col(f.ID).Val
End Sub
输出
AAA
BBB