Skip to content

Est. 2011ยทMicrosoft Partner 7033487ยทDelivery under 3 minยทSupport 7 days a week

Your vault is empty.

Free Fix 1004

Excel VBA Error 1004 Application-Defined or Object-Defined Error: Ranges and Sheets

11 min read Updated October 4, 2026 Outlook & Office Applications

Fix it now

1004 is Excel refusing the call, not VBA failing. Microsoft describes the message as what VBA shows when an error raised by a host application has no VBA-defined number – which is why one number covers a dozen faults. Read the failing line, then find out what it was pointing at.

  1. Click Debug. The highlighted line is the one Excel refused; read it before changing anything.
  2. Open the Immediate window with Ctrl+G and ask what the macro was actually working on: ?ActiveWorkbook.Name, then ?ActiveSheet.Name.
  3. Confirm the sheet exists, spelled exactly: ?ThisWorkbook.Worksheets(“Data”).Name. Error 9, subscript out of range, here means the name is wrong.
  4. If the line builds an address from variables, print the address before the failing line. A column index has to be between 1 and 16384, or A to XFD, and a row index between 1 and 1048576 – Microsoft names those bounds as the cause of this error in automation.
  5. Qualify the line: reach the range through an explicit worksheet object rather than a bare Range or Cells.
  6. If the line writes, inserts, deletes or formats, check protection with ?ActiveSheet.ProtectContents. True is your answer.
  7. Set Error Trapping to Break on All Errors in the editor’s options, so nothing further up swallows the failure.

Read Err.Source as well as Err.Number. Microsoft points at it directly: it carries the programmatic id of the application or object that raised the error, which tells you which side refused the call.

If the macro runs from a cold start with a different workbook active, you are done. The next section explains why one number covers so much and how to narrow it fast.

Why it happens

VBA and Excel are two pieces of software talking across a boundary. VBA owns a small set of numbered errors: 9 is “Subscript out of range”, 13 is “Type mismatch”, 6 is “Overflow”. Everything else is Excel answering back. Microsoft’s own description of the message is precise: it is displayed when an error raised with the Raise method or the Error statement does not correspond to an error VBA defines – so it may be an error you defined, or one defined by an object, including host applications like Excel. The number tells you the object model rejected the call. It does not tell you why.

The second source of confusion is implicit binding. Range(“A1”) does not mean a fixed cell. It means A1 on whichever worksheet is active at the instant that line runs, in whichever workbook is active, and Sheets(“Data”) means the sheet in the active workbook rather than the one the code lives in. A macro that works with its own workbook in front of you fails the moment it is called from elsewhere.

When the caller is another application driving Excel, the same refusal arrives as an HRESULT. 0x800A03EC is that face of it, and Microsoft’s documented example is worth memorising because it is so easily checked: a column index outside 1 to 16384, or a row index outside 1 to 1048576. If your code assembles addresses from variables, test that first. A separate code, 0x80010001 (RPC_E_CALL_REJECTED), is what Microsoft points at for Excel being mid-edit or blocked by a dialog, so if that is your symptom you are looking at the wrong number.

An unqualified reference binds to whatever is active

You have this one if It works with its own workbook in front of you and fails when called from elsewhere or run unattended.

  1. Put Option Explicit at the top of every module so undeclared variables are caught at compile time.
  2. Declare and set explicit workbook and worksheet objects, then reach everything through them.
  3. Use ThisWorkbook for the workbook the code lives in, and a variable for any other workbook you opened, and remove Select and Activate entirely.

A recorded macro is a transcript of your clicks, so it is full of Select and Activate. Rewriting those lines to act on objects directly is usually the whole fix.

Dim wb As Workbook
Dim ws As Worksheet

Set wb = ThisWorkbook
Set ws = wb.Worksheets("Data")

ws.Range("A1").Value = 1
ws.Range(ws.Cells(2, 1), ws.Cells(100, 5)).ClearContents

The sheet, name or range does not exist under that name

You have this one if Error 9 on a Sheets line, or 1004 on a named range that was renamed or deleted.

  1. List the real names: For Each ws In ThisWorkbook.Worksheets: Debug.Print ws.Name: Next
  2. Look for trailing spaces, and for a capital I standing in for a lower case l. Names are compared as text.
  3. Open Formulas > Name Manager: a name pointing at deleted cells shows a reference error and fails every time.
  4. Address sheets by name or code name, not by index. Index numbers shift when sheets are moved.

The address being built is not a valid address

You have this one if The failing line assembles an address from variables, and only some rows or some source files trigger it.

  1. Print the address string immediately above the failing line and read what actually went in.
  2. Guard against a row or column of zero: an empty variable used as an index produces an address Excel cannot accept.
  3. Check the bounds Microsoft names: columns 1 to 16384 or A to XFD, rows 1 to 1048576.
  4. Prefer Cells(row, col) to joining letters and numbers into a string.

The sheet, workbook or range is protected

You have this one if Reads work; only lines that write, format, insert or delete fail, and doing the same by hand asks for a password.

  1. Confirm it with ?ActiveSheet.ProtectContents in the Immediate window.
  2. Unprotect before writing and reprotect afterwards, supplying the password if one is set.
  3. If the code adds or deletes sheets, check workbook structure protection as well; it is a separate lock.
  4. Protection applied with the user-interface-only option is not saved with the file and must be reapplied on open.

Excel is busy and refuses the call

You have this one if The failure comes from another application driving Excel, or from an event handler, and the code you see is 0x80010001 or an unrecognised 0x800A-series value.

  1. Make sure no cell is left in edit mode. One cell with the cursor in it suspends the whole object model.
  2. Close any modal dialog, including a print dialog or an add-in window waiting for input.
  3. From external automation, treat this as transient: back off briefly and retry rather than failing the job.

This is why unattended automation of desktop Excel is fragile: anything that puts it into a modal state stops the caller dead.

Full reference

Reading the failing line

What you see Where the fault is
Error 9 on a Sheets or Workbooks line That name or index is not in the collection right now
1004 on a Range built from variables The address is empty, malformed, or outside the sheet’s bounds
1004 only when started from a different workbook An unqualified reference is binding to the wrong active object
1004 on a line that writes, inserts or formats The sheet or the workbook structure is protected
Error 13 or error 6 on an assignment or counter A cell holds text where a number is expected, or an Integer variable was passed a value above 32767

The bounds worth having in your head

Index Valid range
Column number 1 to 16384
Column letter A to XFD
Row number 1 to 1048576

Microsoft publishes those as the fix for this error in automation, and they catch a specific and common bug: a loop counter that starts at zero, or a variable that was never assigned and therefore holds zero. There is no row zero and no column zero, so the first iteration fails and every subsequent theory about the workbook is wasted effort.

Which numbers are published and which are not

  • 9, 13 and 6 are VBA’s own, with published messages: Subscript out of range, Type mismatch, Overflow.
  • The message “Application-defined or object-defined error” is documented by Microsoft; the number 1004 is not in Microsoft’s list of trappable errors.
  • 0x800A03EC is documented in Microsoft’s Excel automation troubleshooting, with the row and column bounds as its cause.
  • 0x800AC472 has no Microsoft-published meaning at all. Treat it as Excel refusing the call and diagnose it the same way.
  • 0x80010001, RPC_E_CALL_REJECTED, is the one Microsoft names for Excel being mid-edit or blocked by a dialog.

When it only fails in production

Almost always because the environment differs rather than the code. A different workbook is active, a sheet was renamed on their copy, a named range exists only in your file, the file opened read-only from a mail attachment, or protection is applied on the copy in circulation. Reproduce it by opening the macro’s workbook alongside a second, unrelated workbook, making the second one active, and running the macro. Half of these faults appear immediately.

Getting the debugger to tell the truth

  • Comment out any On Error Resume Next on the path. It moves the failure to a later line where the state is already wrong.
  • Set Error Trapping to Break on All Errors while you diagnose.
  • Print the workbook name, the sheet name and the address on the line above the failure, rather than inferring them.
  • Run it twice without closing Excel, to catch state the first run left behind.

Every code this article covers

Code What it points at Source
1004 The message “Application-defined or object-defined error”: VBA displays it when an error raised by a host application or by Err.Raise has no number VBA defines. The number itself is not in Microsoft’s trappable errors list Microsoft Learn
0x800A03EC The COM exception Excel returns when it rejects an object-model call; Microsoft’s documented example is a row or column index outside the sheet’s bounds Microsoft Learn
0x800AC472 Seen from external automation when Excel refuses a call; no meaning is published for it. Microsoft’s documented code for Excel being mid-edit or blocked by a dialog is 0x80010001 not published by the vendor
9 Subscript out of range Microsoft Learn
13 Type mismatch Microsoft Learn
6 Overflow Microsoft Learn

Confirm the fix worked

  1. Run the macro from a cold start of Excel with a different workbook active; it should still complete.
  2. Step through with F8 once to the end, watching the Immediate window output.
  3. Confirm no Select or Activate remains on the code path that failed.
  4. Run it twice in succession without closing Excel, to catch state left behind by the first run.
  5. Run it against the file the person who reported it was using, not only your own test file.

Questions people ask about this

Do I need a different Office licence, or a reinstall, to fix error 1004?

No. This costs nothing. It is a fault in the code or in the workbook it runs against, not in your installation or your entitlement. Repairing Office is wasted effort here.

Why does the same macro work on my machine but not a colleague’s?

Usually a different workbook was active, a sheet was renamed on their copy, a named range exists only in your file, or their copy is protected or opened read-only. Compare the sheet names first, then check protection.

Could On Error Resume Next be hiding the real problem?

Frequently. It moves the failure to a later line where the state is already wrong, so the line you are shown has nothing to do with the bug. Comment it out while you debug and put it back only around the one statement that needs it.

It only fails when the workbook comes from SharePoint or an email.

Files from those sources open in Protected View or as read-only copies, so any write fails. Save to a local trusted location first and run the macro against that copy.

Is 1004 documented anywhere I can point a colleague at?

The message is: Microsoft documents “Application-defined or object-defined error” and explains that it appears when an error raised by a host application has no VBA-defined number. The number 1004 itself is not in Microsoft’s trappable errors list, so treat any page that gives it a confident one-line definition with suspicion.

Related error codes

Was this article helpful?

Your feedback helps us improve our documentation.

Related articles

Free Fix Office Crash on Launch: Event ID 1000 and 0xc0000005 Access Violation Explained Free Fix VBA Error 462: The Remote Server Machine Does Not Exist or Is Unavailable Free Fix Word experienced an error trying to open the file: Recover a Damaged Document License Error Outlook 0x8004060C: The Mailbox or Outlook Data File Has Hit Its Size Limit
โ† Back to Knowledge Base