Excel 2010 VBA - 错误:对象变量或 With 块变量未设置

3

我有一个自己的类(ClassFoo),里面有一个简单的属性(pName),但是我无法设置它,因为总是出现错误...

Class Modules - ClassFoo
---
Public pName as String

Public Property Let Name(Value as String)
   pName = Value
End Property
----
Somewhere else in the ModuleX
...
Dim Foo as ClassFoo
Foo.Name = "foo" <- throws error 
Foo.pName = "foo" <- throws error 

或者

With Foo
.pName = "foo" <- throws error 
End With 

我将“Instancing”类从“Private”更改为“PublicNotCreatable”,然后又改回了原来的设置,但我仍然遇到相同的错误...
提前感谢您的回复。
Cs
2个回答

8

您需要创建一个实例并将其分配给Foo,如下所示:

Dim Foo as ClassFoo
Set Foo = new ClassFoo

2
我相信你需要实例化它,请尝试。
 Dim foo as new ClassFoo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接