VBA Excel: Compile Error: Object required?

calendar_today Asked Oct 11, 2011
thumb_up 16 upvotes
history Updated April 14, 2026

Direct Answer

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…. This is a prose walkthrough, ranked #30th of 95 by community upvote score, from 2011.


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.

help
Frequently Asked Questions

Is this above-median answer still worth copying?
expand_more

Answer score +16 vs the VBA Core archive median ~5; this entry is solid. The score plus 9 supporting upvotes on the question itself (+9) means the asker and 15 subsequent voters all validated the approach.

The answer has no code block — how do I turn it into a snippet?
expand_more

Use the walkthrough above as a checklist, then open a top-10 VBA Core archive entry for a concrete starting template you can adapt.

This answer is 15 years old. Is it still relevant in 2026?
expand_more

Published 2011, which is 15 year(s) before today’s Office 2026 build. The VBA Core object model has had no breaking changes in that window. Three things to re-test: (1) blocked macros on downloaded files (Mark-of-the-Web), (2) 64-bit API declarations (PtrSafe, LongPtr), (3) any shift toward Office Scripts for web scenarios.

Which VBA Core pattern ranks just above this one at #29?
expand_more

The pattern one rank above is “VBA code to set date format for a specific column as "yyyy-mm-dd"”. If your use case overlaps, compare both before committing.

Data source: Community-verified Q&A snapshot. Q-score 9, Answer-score 16, original post 2011, ranked #30th of 95 in the VBA Core archive. Last regenerated April 14, 2026.

vba