Error 1004 — “Application-defined or object-defined error” — is Excel's generic VBA error. Excel is telling you the macro asked for something that doesn't exist, isn't allowed right now, or isn't valid. It is almost never random, and it is almost always fixable in the code or the workbook — not with a downloaded tool.
Unlike specific errors (9 = subscript out of range, 13 = type mismatch), 1004 is the catch-all Excel raises when an operation on a workbook, worksheet, range, chart, or PivotTable fails. The same error number covers several different causes, which is why one-size-fits-all “fixes” don't work. The job is to find which 1004 you have — and the macro itself will tell you, if you ask it the right way.
? variableName in the Immediate window to inspect them.Cause 1The macro refers to a sheet or range that isn't there.
A worksheet was renamed or deleted, a named range moved, or the name in the code doesn't exactly match the sheet tab — including stray spaces. Excel treats "Report " and "Report" as different sheets.
Worksheets("Report").Range("A1") ' fails if the tab is named "Report " or "Reports"
Fix: compare the name in the code character-for-character with the tab. Better, reference sheets by their code name or check existence before using them, and fully qualify references so the macro can't silently point at the wrong workbook:
ThisWorkbook.Worksheets("Report").Range("A1").Value = 5
Cause 2The sheet or workbook is protected.
Writing to cells, sorting, filtering, or changing a PivotTable on a protected sheet (or in a protected workbook structure) raises 1004 even when the reference is perfect. Recording a macro on an unprotected sheet and replaying it on a protected one is the classic setup.
Fix: unprotect before the operation and re-protect after — in the sheet UI, or in code with ws.Unprotect / ws.Protect (with the password if one is set). If the file belongs to someone else, ask them — don't work around their protection.
Cause 3The range reference isn't valid.
The code builds a range address that Excel can't parse, or points outside the grid (rows run 1–1,048,576; columns A–XFD). Concatenated addresses are the usual suspect — a variable comes back empty or zero and the address collapses into nonsense.
Range("A" & lastRow) ' fails with 1004 if lastRow is 0 or empty
Fix: print the built address in the Immediate window (Debug.Print address) and check it by eye. Guard the inputs: if the row or column variable can be empty, handle that before building the reference.
Cause 4The code leans on Select / Activate and the selection isn't what it assumes.
Recorded macros act on whatever happens to be selected. Run them with a chart selected, a different sheet active, or multiple sheets grouped, and innocuous lines fail with 1004.
Fix: replace Select-based code with direct references — instead of selecting a sheet and then a cell, address the cell through its sheet (Cause 1's pattern). This removes a whole family of 1004s at once and makes the macro faster.
Cause 5The copy-a-worksheet 1004.
A known Excel quirk: copying a worksheet many times in one session (manually or by macro) eventually fails with 1004 even though nothing is wrong with the code.
Fix: save the workbook, close Excel completely, reopen, and run it again. If a macro copies sheets in a loop, have it save-and-continue in batches rather than hundreds of copies in one session.