将集合添加为另一个集合中的项目 – 类 – Excel VBA
我目前正在尝试构build一个集合,其中一个集合可能包含另一个集合作为一个Item。
我设置了两个集合,并为每个集合创build一个类模块:col1 – (链接到Class1); 和col2 – (连接到Class2)
以下是我的class级模块:
1类:
Option Explicit Private pTestC1A As String Private pTestC1B As Collection Public Property Let TestC1A(Value As String) pTestC1A = Value End Property Public Property Get TestC1A() As String TestC1A = pTestC1A End Property Property Set TestC1B(col2 As Collection) Set pTestC1B = col2 End Property Property Get TestC1BElements(v As Integer) As String TestC1B = pTestC1B(v) End Property
等级2:
Option Explicit Private pTestC2A As String Public Property Let TestC2A(Value As String) pTestC2A = Value End Property Public Property Get TestC2A() As String TestC2A = pTestC2A End Property
以下是我的模块代码
Sub Test() Set col1 = New Collection Set col2 = New Collection Set cV = New Class1 cV.TestC1A = "First Collection" Set aV = New Class2 aV.TestC2A = "Second Collection" sKey1 = CStr(aV.TestC2A) col2.Add aV, sKey1 Set cV.TestC1B = col2 sKey2 = CStr(cV.TestC1A) col1.Add cV, sKey2 If Err.Number = 457 Then MsgBox "Error Occured" ElseIf Err.Number <> 0 Then Stop End If Err.Clear Msgbox col1(1).TestC1A ' works fine Msgbox col2(1).TestC2A ' works file MsgBox col1(1).TestC1B(1).TestC2A ' does not work - 450 run-time error End Sub
根据上面的代码,如果我分别引用每个集合,我可以成功获取这些项目的值,但是如果我尝试获取“错误数量的参数或无效的属性分配”运行时错误项目值以嵌套的方式。
如果有人能够帮助指出我出错的地方,那么可以理解,也许可以在类模块处理集合的属性集和获取参数的方式上find一些启示。
您在Class1
类模块中缺lessGet TestC1B
属性:
Property Get TestC1B() As Collection Set TestC1B = pTestC1B End Property
一旦存在,你就可以调用col1(1).TestC1B(1)
并访问它的.TestC2A
属性
背景
通过在类中使用私有variables并使用属性为您的私有variables提供读取/写入权限,您做了正确的事情。 这样你会得到更多的控制。
属性Get
给予该属性的读取权限(以及广义地说,基本私有variables)。 例如,您可以使用Range.Address
来返回( 读取 )范围对象的地址。
Property Let
和Set
给予该属性的写权限。 使用Set
为对象。 例如, Range.Value = 1
会将新值写入范围。
考虑一下, Range.Address = $A$1
。 由于范围的地址没有Property Set
,所以这不会改变范围的地址。 它将认为Range.Address
部分是一个Get
调用,并在此示例中评估$A$1 = $A$1
返回TRUE
- 我如何打开一个访问文本文件,并加载在VBA Excel中的文件path存储在一个variables?
- 无法通过JScript更新TestComplete中的Excel工作表
- InputBox:只允许一定范围的值
- 使用变长主题和退货的中间和左侧函数的Excel – 分割内容
- 导出excel的functionjavascript,要保留在手机栏上
- EXCEL VBA – search目录中的每个文件,列出search到的string实例的数量
- 在Excel VBA中将图像(jpg)转换为base64?
- 写入数据在Java中第一个选项卡数据不会显示,而写第二个选项卡
- Excell IF(ISBLANK),但仍然获得-42716