Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Excel: Compile Error: Object required?

Tags:

excel

vba

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
like image 829
UllaDieTrulla Avatar asked Oct 11 '11 10:10

UllaDieTrulla


2 Answers

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 declares refData of type Inherit to declare them both on one line you must; Dim Data As Inherit, refData As Inherit

like image 66
Alex K. Avatar answered Nov 17 '22 20:11

Alex K.


Dim Data, refData As Inherit

declares Data as Variant, only refData as 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 Set in the assignment, since Inherit 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/

like image 21
Doc Brown Avatar answered Nov 17 '22 20:11

Doc Brown