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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7163138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:52:02  来源:igfitidea点击:

Initialize vba class member variable with Range

excelvbaobjectconstructorinitialization

提问by postelrich

I have created a class with one of the member variables is of type range. Now if I try to initialize or set the value of that range I error: Object Variable or with block variable not set. Now I thought it was because it get initialized as Nothing but if I use the class sub Class_Initialize and try to set a default there, it still errs. So what gives?

我创建了一个类,其中一个成员变量的类型为 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

回答by Doug Glancy

You need to use the Set keyword to initialize or set the range, e.g., :

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

Set Rng = pRng
Set pRng = Value