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

csc*_*aba 2 excel vba excel-2010

我有一个自己的类(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 
Run Code Online (Sandbox Code Playgroud)

要么

With Foo
.pName = "foo" <- throws error 
End With 
Run Code Online (Sandbox Code Playgroud)

我将班级'Instancing'从'Private'更改为'PublicNotCreatable'(来回)但我仍然有同样的错误......

感谢您提前回复.

Ale*_* K. 6

您需要创建一个实例并将其分配给它Foo;

Dim Foo as ClassFoo
Set Foo = new ClassFoo
Run Code Online (Sandbox Code Playgroud)