Excel VBA Compile Error: Object Required Explained

VBA & macros · Updated November 19, 2026

In Excel VBA, the compile error “Object required” usually means a statement expects an object but is being given a value such as text, a number, or a cell’s contents. Check the highlighted line for an incorrect Set assignment or a With block aimed at a value, then use object assignment or ordinary value assignment as appropriate.

What “Object Required” Means in VBA

VBA distinguishes objects from the values they contain. A worksheet, range, workbook, chart, and collection are objects: they have properties and methods, and code can refer to them as entities. A cell’s text, number, date, or Boolean result is a value. The distinction matters because certain VBA statements require an object expression, not just any expression.

For example, Range("A1") refers to a Range object, while Range("A1").Value refers to the value stored in that cell. If A1 contains the word “North,” the second expression returns that text. Code that expects an object cannot use that text in its place.

The message is not a diagnosis of one single mistake. It identifies a mismatch somewhere in the code being compiled. Start with the highlighted statement, then check the expressions on both sides of assignments and the expression following With. The surrounding declarations and earlier assignments may also explain why the expression has the type it does.

Compile Error Versus Runtime Error 424

A compile error prevents VBA from compiling the project, so the affected procedure cannot run until the code is corrected. In the Visual Basic Editor, use Debug > Compile VBAProject to ask VBA to check the project. If compilation stops, the editor generally highlights a statement related to the problem. The exact cause may be in that statement or in the way one of its variables is declared.

Do not confuse this with runtime error 424, which is also called “Object required.” A runtime error occurs after a procedure has started running. The code may have compiled successfully, but execution reaches an expression that needs an object and does not have one available. The remedy depends on whether the problem is a compile-time type mismatch or a runtime reference problem.

Other messages point to different issues. “Object variable or With block variable not set” (runtime error 91) usually means an object variable has not been assigned a usable object. “Variable not defined” often indicates a misspelled or undeclared variable when Option Explicit is enabled. Fixing the actual message is more reliable than changing code based on a similar-sounding error.

Check Set Assignments and Cell Values

Use Set when assigning an object reference to an object variable. Do not use it when assigning a property value such as a cell’s contents. These two patterns look similar, but they do different things:

Sub ShowObjectAndValue()
    Dim target As Range
    Dim cellValue As Variant

    ' Assign the Range object to an object variable.
    Set target = ThisWorkbook.Worksheets("Sheet1").Range("A1")

    ' Assign the cell's contents as a value, not an object.
    cellValue = target.Value
End Sub

In this example, target refers to the cell itself, so it can be used to access properties such as .Value. The cellValue variable holds whatever is in the cell. If you change the first assignment to Set cellValue = target.Value, VBA is being asked to treat a value as an object, which is a common cause of the compile error.

The reverse mistake is also common: leaving out Set when assigning an object variable. For instance, a variable declared As Range needs an object reference assigned with Set. A variable holding a string, number, date, or other ordinary value uses a normal assignment without Set. The declared type is a useful clue when deciding which form to use.

Use With Blocks on Objects, Not Their Contents

A With block lets you refer to one object repeatedly without writing its full name each time. The expression immediately after With must evaluate to an object. A range is an object; the value stored in a range is not necessarily one.

For example, With Range("A1").Value is wrong when the intention is to format the cell, because .Value supplies the cell’s contents. Use the range object instead:

Sub FormatOneCell()
    Dim target As Range

    ' Save a copy of the workbook before running a macro; Ctrl+Z cannot undo VBA changes.
    Set target = ThisWorkbook.Worksheets("Sheet1").Range("A1")

    ' The With expression is a Range object.
    With target
        .Value = "Ready"
        .Font.Bold = True
    End With
End Sub

This example writes text into A1 and makes its font bold. The leading dots inside the block refer to properties of target. Without those dots, a name such as Value would not be qualified as a member of the object in the block. A missing dot can produce a different compile message depending on the statement, so check both the With expression and the statements inside it.

A Practical Way to Find the Fault

Work from the first compile failure rather than editing several statements at once. VBA may not report every issue in a project until earlier compile problems have been fixed. A simple, methodical check often reveals the mismatch quickly:

  1. In the Visual Basic Editor, choose Debug > Compile VBAProject. Read the complete message and note the highlighted line.
  2. Identify every expression on that line that should be an object: a worksheet, range, workbook, chart, or other object reference.
  3. Check each variable’s declaration. Does it say As Range, As Worksheet, or another object type, or does it hold a value such as String or Long?
  4. Check assignment syntax. Use Set for an object reference; omit it when assigning a value. Confirm that the right-hand side is the type the variable expects.
  5. For a With statement, remove trailing properties such as .Value if they turn the object expression into a value.
  6. Compile again after each correction. Once compilation succeeds, run the relevant procedure and check separately for runtime errors or incorrect results.

If the highlighted line appears valid, inspect the procedure’s declarations and the expression that supplies its object. A variable may have been declared as the wrong type, or a function may return a value where the caller expects an object. Also check for a line continuation or preceding statement that changes how VBA reads the code. Compilation helps narrow the location, but the underlying mismatch can depend on context.

Common Causes Beyond a Simple Set Mistake

Incorrect object declarations are another source of confusion. A variable declared as String cannot serve as a Range object just because it contains an address such as “A1.” The text must first be used to obtain a Range object, and the object must be assigned to a variable declared for an object. Likewise, if a procedure is intended to return an object, its return value and the caller’s assignment need to use object-compatible declarations and syntax.

Properties can also obscure the type of an expression. A worksheet’s Range("A1") property returns a Range object, but that range’s Value property returns cell data. Follow the expression from left to right and ask what each property or function returns. A chain that starts with an object can end in a value, so the first part being an object does not make the whole expression one.

Some errors that appear related are not “Object required.” If a declaration uses a type VBA does not recognize because a library reference is missing, the message may instead be “User-defined type not defined.” A misspelled variable can lead to “Variable not defined” when Option Explicit is active. Those errors have different fixes; changing Set blindly will not repair a missing type definition or a spelling mistake.

Object References and Workbook Context

Once the syntax is corrected, make object references specific enough for the task. Unqualified references such as Range("A1") can depend on the active sheet. That may be convenient in a short macro, but it can point at the wrong worksheet if another sheet is active when the macro runs. Qualifying the range with a workbook and worksheet makes the intended object clearer and reduces surprises.

ThisWorkbook means the workbook that contains the VBA code; ActiveWorkbook means the workbook currently active in Excel. They can be different, particularly when a macro in one file works with another open file. Pick the reference that matches the goal, then qualify the worksheet and range from it. A valid object expression can still target the wrong workbook, so correctness includes both type and context.

When object libraries are involved, such as Outlook automation, a missing or unavailable reference is a separate concern from an object/value mismatch. For code that needs to run across varied Office installations, late binding with a variable declared As Object can avoid a compile-time dependency on a specific library, but it also removes some editor assistance and compile-time type checking. It is not a general fix for an invalid Set expression.

Preventing the Error and Working Safely

Clear declarations make object/value mistakes easier to spot. In a procedure, give object variables meaningful names such as sourceSheet or destinationRange, and give scalar variables names such as lastRow or statusText. Add Option Explicit at the top of modules so undeclared variable names are caught during compilation instead of silently becoming Variants.

Compile after meaningful code changes, especially after changing a variable’s declaration, a function’s return type, or a With block. Keep object acquisition separate from value processing: first assign a worksheet or range to an object variable, then read or write its properties. This makes each line’s purpose visible and helps distinguish an object reference from the data inside it.

Before running a macro that changes a workbook, save a separate copy. VBA actions generally cannot be reversed with Excel’s Ctrl+Z undo stack. On Windows, Alt+F11 opens the Visual Basic Editor in Excel; on Mac, the shortcut is commonly Option+F11, though the Fn key may also be needed depending on the keyboard’s function-key settings. Excel for the web cannot run or edit VBA macros; use the desktop Excel application.

Frequently Asked Questions

What does “Compile error: Object required” mean in Excel VBA?
It means VBA found a statement that requires an object but received something that is not an object, often a cell value. Check object assignments, With expressions, and variable declarations on the highlighted line.

How do I fix “Object required” when using Set?
Confirm that the expression after Set returns an object and that the receiving variable is declared as an object type. If you meant to store a cell’s contents, remove Set and assign the value to a suitable value variable.

Should I use Set when assigning a cell value?
No. Use Set to assign an object such as a Range to an object variable. Assign the result of Range("A1").Value without Set, because it is the cell’s data, not the Range object.

Why does my With statement say Object required?
The expression after With must be an object. If it ends in a property such as .Value, it may evaluate to text or a number instead. Use the Range, Worksheet, or other object itself when the block needs to access that object’s members.

Is runtime error 424 the same as a compile error?
No. Both use the words “Object required,” but a compile error prevents code from compiling, while runtime error 424 occurs during execution. Fix the type or syntax mismatch for the compile error; for runtime 424, trace the object expression reached while the procedure runs.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.