The Problem (Q-score 9, ranked #30th of 95 in the VBA Core archive)
The scenario as originally posted in 2011
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
Why the Win32 API declaration is fragile here
This problem involves a Declare statement, which means 32-bit vs 64-bit compatibility is in play. Office 64-bit requires the PtrSafe keyword and LongPtr data types for any handles — the most common root cause of the exact symptom described.
The Verified Solution — solid answer (above median) (+16)
Verbal answer — walkthrough without a code block
Note: the verified answer is a prose walkthrough. If you need a runnable sample, check VBA Core entries ranked in the top 10 of the same archive.
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
When to Use It — vintage (14+ years old, pre-2013)
Ranked #30th in its category — specialized fit
This pattern sits in the 87% tail relative to the top answer. Reach for it when your scenario closely matches the question title; otherwise browse the VBA Core archive for a higher-consensus alternative.
What changed between 2011 and 2026
The answer is 15 years old. The VBA Core object model has been stable across Office 2013, 2016, 2019, 2021, 365, and 2024/2026 LTSC, so the pattern still compiles. Changes that might affect you: 64-bit API declarations (use PtrSafe), blocked macros in downloaded files (Mark-of-the-Web), and the shift toward Office Scripts for web-first workflows.