VBA Excel:编译错误:需要对象吗?
我在标记行中收到 VBA Excel“编译器错误:需要对象”错误。 我不明白原因。
顺便说一句:希望 Excel 支持 .Net 语言而无需包装器。
Option Explicit
Public Type Inherit
ReqId As Integer
Parent As Integer
Depth As Integer
Path As String
End Type
Sub test()
Dim MyStructure() As Inherit
ReDim MyStructure(1 To 1000)
MyStructure(1).ReqId = 1
Dim Data, refData As Inherit
Set Data = MyStructure(1) ' <---!
Beep
End Sub
I get a VBA Excel 'Compiler Error: Object required'-Error in the marked line.
I do not understand the reason.
BTW: Wish Excel would support a .Net language without wrapper needs.
Option Explicit
Public Type Inherit
ReqId As Integer
Parent As Integer
Depth As Integer
Path As String
End Type
Sub test()
Dim MyStructure() As Inherit
ReDim MyStructure(1 To 1000)
MyStructure(1).ReqId = 1
Dim Data, refData As Inherit
Set Data = MyStructure(1) ' <---!
Beep
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Set
用于对象的赋值,用户定义的类型被视为常规变量,因此使用=
进行赋值。另外(令人困惑);
Dim Data, refData As Inherit
仅声明Inherit
类型的refData
,必须在一行上声明它们;将数据设为继承,refData 作为继承
Set
is for the assignment of objects, a user defined type is treated like a regular variable so use=
to assign.Also (confusingly);
Dim Data, refData As Inherit
only declaresrefData
of typeInherit
to declare them both on one line you must;Dim Data As Inherit, refData As Inherit
将
Data
声明为Variant
,仅将refData
声明为Inherit
。做你想做的事。 VBA语法在这里不是“常识”,我已经看到这个错误几十次了。编辑:当然,您必须在分配中省略
Set
,因为Inherit
是用户定义的类型。如果您正在寻找免费且易于使用的 Excel .NET 集成,请查看 Excel-DNA:
http://exceldna .codeplex.com/
declares
Data
asVariant
, onlyrefData
asInherit
.does what you want. The VBA syntax is not "common sense" here, I have seen this error dozens of times. EDIT: of course, you will have to leave out
Set
in the assignment, sinceInherit
is a user defined type.If you are looking for a free and easy to use .NET integration for Excel, look at Excel-DNA:
http://exceldna.codeplex.com/