在VBA中构建类模块的新手,并且努力填充在另一个对象中定义的一个对象。
例如,我有两个类模块,Class1和Class2
的Class1
Dim mobj As Class2
Public Property Set SetObj(obj As Class2)
Set mobj = obj
End Property
的Class2
Public FirstName As String
可从标准模块访问这些内容,如下所示:
Sub test()
Dim X As Class1
Set X = New Class1
Set X.SetObj = New Class2
X.SetObj.FirstName = "Bruce"
End Sub
当我收到“无效使用属性消息”时,这在X.SetObj.FirstName =“Bruce”中失败了。任何帮助将不胜感激。
答案 0 :(得分:3)
你很亲密。 的Class1:
Private mobj As Class2
Public Property Set Obj(Obj As Class2)
Set mobj = Obj
End Property
Public Property Get Obj()
Set Obj = mobj
End Property
Sub(X.Obj.FirstName行调用Get属性,而不是Set属性):
Sub test()
Dim X As Class1
Set X = New Class1
Set X.Obj = New Class2
X.Obj.FirstName = "Bruce"
End Sub