VBA – 由于某些原因,使用get / let属性不会设置

警告:我是VBA的新手。

我有以下类,名为TestClass

 Option Explicit Private pName As String Private Sub Class_Initialize() Name = "test_1" End Sub Public Property Get Name() As String Name = pName End Property Public Property Let Name(Value As String) pName = Value End Property 

我有一个子程序,我连接到一个button单击事件,看起来像这样:

 Sub DisplayTestValue() Dim testClass As testClass With testClass MsgBox "default value: " & testClass.Name End With testClass.Name = "Tom" MsgBox "set to (Tom) value: " & testClass.Name End Sub 

然后我点击button。 它调用子程序。 我收到以下错误信息:

 Run-time error '91': Object variable or With block variable not set 

debugging器在以下代码行停止:

 MsgBox "default value: " & testClass.Name 

debugging器告诉我,testClass.Name没有设置! 我很困惑,因为我设置默认值“test_1”到类初始化程序中的名称属性。

有人可以向我解释我做得不对吗?

我find了答案

实例化我的TestClass对象时,我没有使用关键字New

坏:

 Dim testClass As testClass 

好:

 Dim testClass As New testClass