This error occurs when a VBA macro in Excel, Access, or Word crashes with "Runtime Error 31037: Error loading from file." The core issue is that the macro fails to reference a valid, accessible file path, corrupt macro code, or a permissions problem. Here’s how to diagnose and fix it step-by-step.

What You'll Need

  • A working version of Microsoft Office (Excel, Access, or Word) installed.
  • The file or module that triggers the error (e.g., a workbook, VBA project, or text file).
  • Administrator or user-level permissions to modify file paths or permissions.
  • Access to the VBA editor (Alt+F11 in Excel or the Developer tab in Word).

Step-by-Step

  1. Identify the problematic file: Check which macro is failing by pressing Alt+F8 in Excel to list all macros. Select the one causing the error and press F8 to step through it. If the error occurs during execution, note the line number where it stops.
  2. Verify the file path: Open the VBA editor (Alt+F11). Look at the line where the error occurs—it will likely be referencing a file like: Workbooks.Open("C:\Path\To\File.xlsx") or Dim myFile As String: myFile = "C:\Path\To\MissingFile.txt". Ensure the file exists at that exact path. If the path is relative (e.g., "./data.csv"), test whether the file is in the correct subfolder.
  3. Check for typos or corruption: If the file doesn’t exist, correct the path or add error handling around the file operation. For example: On Error Resume Next Workbooks.Open("C:\Path\To\File.xlsx") If Err.Number <> 0 Then MsgBox "File not found!" End If If the error persists, the file might be corrupted. Retry opening it manually in the host program (Excel/Word/Access).
  4. Update or repair Microsoft Office: Run the Office Installer (e.g., Office Setup) with the option to repair or upgrade components. This ensures all VBA components (like COM libraries) are current.
  5. Recompile the VBA code: If the error is in macro logic (e.g., a syntax error or circular reference), compile the project: 1. Open the VBA editor. 2. Press Ctrl+Alt+F11 to compile all macros. 3. If errors appear, fix them line by line.
  6. Test file permissions: Right-click the VBA project file (e.g., `.xlsm`) and check its properties. Ensure the user account has read/execute permissions. If the error occurs when reading a text file, verify the file isn’t locked by another process.
  7. Recreate the module: If the error occurs in a custom module, back up the code, delete the module, and recreate it. Paste the original code back in. This sometimes resolves hidden corruption.
  8. Check for antivirus interference: Temporarily disable real-time antivirus scanning for the file or Excel folder. Some AV tools flag legitimate VBA files as threats, causing runtime failures.

If That Did Not Work

If none of the above steps resolve the issue, proceed with these harder fixes:
  1. Restore from backup: Recover the original workbook or VBA project from a backup. If the file is corrupted, this is the safest solution.
  2. Reinstall Office: Uninstall Office, delete user-specific files (e.g., `%AppData%\Microsoft\Office` for Windows), and reinstall. This clears leftover corrupted files.
  3. Create a new VBA project: In Excel, open a blank workbook and add a new module. Write the macro manually. If it works, compare the two versions to find the difference.

Common Mistakes

  • Assuming relative paths work: Relative paths (e.g., "..\data\file.xlsx") fail if the macro runs from a different folder. Use absolute paths or store the path as a variable.
  • Ignoring permissions: Some files (e.g., system files or shared drives) require admin rights. Test permissions with FileSystemObject in VBA.
  • Overlooking circular references: If Module A calls Module B, which calls Module A again, VBA fails to load the project. Use Ctrl+Alt+F11 to check for unresolved references.
  • Using deprecated functions: Older VBA code may rely on functions like GetObject or OpenSharedFile. Update to modern methods like Workbooks.Open or FileSystemObject.

Tips

  • Use Debug.Print to trace file paths in the Immediate Window (Ctrl+G).
  • For large projects, split macros into smaller modules to isolate errors.
  • If the file is on a network drive, ensure the path includes the correct UNC format (e.g., "\\server\share\file.xlsx").
  • Enable macros with a warning prompt first to catch errors early: ThisWorkbook.EnableEvents = False before file operations.

Troubleshooting

I still get the error even after checking all paths and permissions.

  1. Run Excel in Safe Mode: Launch Excel with Safe Mode (no add-ins). If the error disappears, a third-party add-in is corrupting the VBA environment. Disable add-ins one by one to find the culprit.
  2. Check for broken Excel Add-ins: Go to File > Options > Add-ins > Manage COM Add-ins to disable problematic extensions.
  3. Test with a fresh Excel instance: Launch Excel as Administrator. If the error persists, the issue is likely Office-specific and may require reinstallation.

What if the error is in Access?

  1. Access uses a different file format (.accdb/.adp). Ensure the linked tables or queries exist in the correct path. Use AccessDatabaseEngine.x64.msi to update Access if errors persist.
  2. In VBA, use CurrentDb.Execute or DoCmd.OpenQuery with error handling: On Error GoTo ErrHandler CurrentDb.Execute "SELECT * FROM MyTable" Exit Sub ErrHandler: MsgBox "Error: " & Err.Description End Sub

Frequently Asked Questions

Why does VBA load my file from "C:\Temp"?

The file is likely being redirected due to a temporary path override. Check for registry edits (e.g., in HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Excel\Options) or system environment variables that modify working directories.

How do I fix a VBA error if the file is on a USB drive?

Ensure the USB drive is mounted and accessible. In VBA, use a full path like "Z:\Project\data.xlsx" (replace Z: with the actual drive letter). Avoid relative paths that rely on the working directory.

Can I run this macro in a different Office version?

Yes, but compatibility issues may arise. Test in the same Office version as the macro was originally written. Use a tool like VBAProject.pub1 (Excel 2010+) to convert between versions if needed.

Why does my macro fail when run in Word but works in Excel?

The error often stems from differences in the host program’s environment. In Word, paths or references may not resolve correctly due to default folders or macros running in a sandboxed state. Compare the macro’s behavior between apps.

Is there a way to log file operations in VBA?

Yes, use the Immediate Window or a text file to log errors: Open "C:\Logs\VBA_Errors.txt" For Output As #1 Print #1, "Error at " & Now & ": " & Err.Description Close #1 End Sub

Does upgrading to Excel 365 fix this error?

Not always. Upgrading may resolve some issues, but deeper problems (e.g., corrupted files, third-party code) persist. Always test new macros in the latest version first.

Final Notes

If the file is shared across multiple users, ensure all accounts have write permissions to the VBA project file. For Access databases, consider exporting macros to a separate solution or hosting them in a web service if permissions are a recurring issue.