Runtime Error 51: “Internal error,” explained and fixed

Error 51, “Internal error”, is VBA admitting the failure is inside its own engine rather than in a statement the code executed. It is rare, and it is deliberately uninformative: the runtime hit a condition it has no specific message for. Corrupted compiled code inside a workbook, a damaged form, and genuine host bugs are the realistic causes, which makes this an environment and file-health problem far more often than a logic bug.

For people who just run the program

  1. Test a different file with the same program or macro set. Error 51 that follows one document points at that document; error 51 everywhere points at the installation.
  2. Restart the application and the computer before deeper steps. Internal engine state can be poisoned by an earlier crash in the same session; a clean start is a real diagnostic here, not a shrug.
  3. Keep a copy of the file exactly as it was. If the file is corrupt, repair attempts change it; the untouched copy preserves the evidence and the data.
  4. Report whether a recent crash preceded it. “It started right after Excel crashed yesterday” is the single most useful sentence for this error.

For people who write the code

  1. Decompile the project. Workbooks carry a compiled copy of the VBA alongside the source, and that compiled copy can corrupt. Opening the file with the host's decompile switch (for Office, starting with /decompile where supported) or exporting and re-importing every module forces a clean rebuild.
  2. Export modules and rebuild the file. Save modules as text, create a fresh workbook, import the modules and copy the data across. If error 51 stays behind in the old file, the file was the fault.
  3. Bisect recent changes. An internal error that began after adding a form or control often traces to that one damaged component; remove it, recompile, and re-add it fresh rather than editing it in place.
  4. Apply Office updates. Genuine engine bugs behind error 51 are fixed in updates; a fully patched host is part of the differential, not an afterthought.
Do not keep developing in a file that throws error 51. Whatever is damaged tends to get worse, and later corruption can take real code and data with it. Migrate the project into a fresh file first, then continue.

If it still happens

If a clean file on a patched host still raises error 51 at the same line, reduce that line to the smallest reproduction: internal errors sometimes trace to one unusual construct (a deeply nested expression, an exotic control) that the engine mishandles. The minimal case is both the workaround guide and the core of a useful bug report to Microsoft.

Common questions

Is error 51 my code's fault? Usually not in the ordinary sense. It signals the VBA engine itself failed. Corrupt compiled state in the file is the most common trigger, which is why rebuilding the file fixes most cases.

Can error 51 corrupt my data? The error itself does not write anything, but the corruption that causes it can spread if the file keeps being used and saved. Work on a copy and migrate to a fresh file promptly.

Related errors

Runtime Error 440 · Runtime Error 7 · Runtime Error 429