使用Range初始化vba类成员变量

时间:2011-08-23 15:00:49

标签: excel vba object constructor initialization

我创建了一个类,其中一个成员变量的类型为range。现在,如果我尝试初始化或设置该范围的值我错误:对象变量或未设置块变量。现在我认为这是因为它被初始化为Nothing但如果我使用类子Class_Initialize并尝试在那里设置默认值,它仍然是错误的。什么给出了什么?

Private pRng As Range
Private pstype As Boolean

Public Property Get Rng() As Range
    Rng = pRng
End Property

Public Property Let Rng(Value As Range)
   pRng = Value
End Property

Public Property Get Stype() As Boolean
   Stype = pstype
End Property

Public Property Let Stype(Value As Boolean)
    pstype = Value
End Property

Private Sub Class_Initialize()
    pRng = Range("A1")
    pstype = True
End Sub

1 个答案:

答案 0 :(得分:4)

您需要使用Set关键字初始化或设置范围,例如:

Set Rng = pRng
Set pRng = Value