VBA Excel:编译错误:需要对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7724930/
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-08 12:02:19  来源:igfitidea点击:

VBA Excel: Compile Error: Object required?

excelvba

提问by UllaDieTrulla

I get a VBA Excel 'Compiler Error: Object required'-Error in the marked line. I do not understand the reason.

我在标记的行中收到 VBA Excel '编译器错误:需要对象'-错误。我不明白原因。

BTW: Wish Excel would support a .Net language without wrapper needs.

顺便说一句:希望 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

回答by Alex K.

Setis for the assignment of objects, a user defined type is treated like a regular variable so use =to assign.

Set用于对象的分配,用户定义的类型被视为常规变量,因此用于=分配。

Also (confusingly); Dim Data, refData As Inheritonly declares refDataof type Inheritto declare them both on one line you must; Dim Data As Inherit, refData As Inherit

也(令人困惑);Dim Data, refData As Inherit仅声明refData类型Inherit以在一行中声明它们,您必须;Dim Data As Inherit, refData As Inherit

回答by Doc Brown

Dim Data, refData As Inherit

declares Dataas Variant, only refDataas Inherit.

声明DataVariant,仅refData作为Inherit

Dim Data As Inherit, refData As Inherit

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 Setin the assignment, since Inheritis a user defined type.

做你想做的。VBA 语法在这里不是“常识”,我已经看到这个错误几十次了。编辑:当然,您必须Set在作业中省略,因为Inherit它是用户定义的类型。

If you are looking for a free and easy to use .NET integration for Excel, look at Excel-DNA:

如果您正在为 Excel 寻找免费且易于使用的 .NET 集成,请查看 Excel-DNA:

http://exceldna.codeplex.com/

http://exceldna.codeplex.com/