The runtime error 5 ("Invalid Procedure Call") in VBA means your macro or subroutine is trying to execute a function, method, or command that doesn’t exist in the object model or the active environment. This error is common when working with Excel, Word, Access, or custom add-ins. The simplest solution is to verify the exact procedure name in your code and ensure it matches what an object actually supports. If that doesn’t work, check dependencies, object references, and module scopes.

What You'll Need

  • An open Excel workbook or Access database (or the target document for Word)
  • Your VBA project editor (File > Options > Current Location in Excel)
  • Access to the VBA editor (press Alt+F11 in Excel)
  • Basic familiarity with VBA syntax (if you’re debugging)
  • A second monitor or window if testing multiple scenarios

Step-by-Step

  1. Close unnecessary applications to avoid conflicts. A second Excel instance, Word, or Access running in the background can interfere with your VBA code. If you’re using Office 365, restart the Office process entirely (Control Panel > Programs > Office > Uninstall or Repair).
  2. Check for typos in procedure names:
    1. Open your project in the VBA editor (press Alt+F11). Navigate to the module containing your error.
    2. Highlight the line with the error and press F9 to break into debugging mode.
    3. Look closely at the exact procedure name being called. Compare it with the documentation for the object class (e.g., "Worksheet" in Excel, "Records" in Access). Example: If you’re trying to use "CallRange" but your code says "CallRange2," correct it to match a supported method.
    Common examples:
    • Excel: "Worksheet.Range" requires "Selection" or "UsedRange"; avoid "InvalidRange."
    • Word: "Selection.Font" works; "Selection.Fonts" is invalid.
    • Access: "DAO.Recordset" must be properly initialized; "InvalidRecordset" will fail.
  3. Verify object references:
    1. If your code references a global object (e.g., "Worksheets"), ensure the object exists in runtime. For example:
    2. If TypeName(ActiveSheet) = "Nothing" Then
                MsgBox "ActiveSheet is null!"
              End If
    3. Check if your code assumes a specific sheet (e.g., "ThisWorkbook.Worksheets("Sheet1")")—it must exist at runtime.
  4. Review scope and declarations:
    1. If you’re using a function within a method, ensure the function is declared before use. Example:
    2. Function GetData() As String
        GetData = "Test"
      End Function
      
      Sub MyMacro()
        Debug.Print GetData() ' Works
      End Sub
      
      Sub WrongMacro()
        Debug.Print WrongFunction() ' Error 5 if WrongFunction isn't declared
      End Sub
    3. For modules, declare procedures at the top with `Private Sub` or `Public Sub`. Avoid naming your procedure the same as a built-in method (e.g., naming a sub "Save" will conflict with the Word/Excel "Save" command).
  5. Test with minimal code:
    1. Replace your entire macro with a single line that calls a known valid procedure. Example for Excel:
    2. Worksheets(1).Activate ' Valid if Sheet1 exists
      ' OR
      ActiveSheet.Range("A1").Value = "Test" ' Valid if A1 exists
      
    3. If this works, the issue is in your original code’s syntax or dependencies.
    4. If it fails, the error is in your environment setup (e.g., missing sheet, incorrect range).
  6. Check for missing dependencies:
    1. In Excel, if you’re using functions like `VBAProject` or `Application`, ensure they’re enabled:
    2. Option Explicit
      ' Enable these at the top of every module if used
      Option Base MSCompat
    3. For external libraries (e.g., COM add-ins), restart Excel after installation and verify they’re listed in: Excel > File > Options > Add-ins > Manage COM Add-ins (Check "Browse" for custom add-ins).
  7. Debug step-by-step:
    1. Press F8 to step through your code line by line. Pause at the error line and check:
      1. What object is being used (e.g., `Worksheets("Sheet1")`) and whether it exists.
      2. The exact method being called (e.g., `Range.Select` vs. `Range.Copy`).
    2. Use the Immediate Window (View > Immediate Window) to print variables:
    3. Debug.Print TypeName(ActiveSheet) ' Removes "nothing" errors
      Debug.Print Sheets.Count ' Shows active sheets

If That Did Not Work

If the error persists after reviewing the above, try these advanced steps in order:
  1. Clear the VBA environment:
    1. Close Excel/Access/Word.
    2. Delete the VBAProject file (e.g., `ThisWorkbook.vbaProject` in Excel) and reopen the file. This resets references but doesn’t delete macros—export your code first if needed.
    3. In Excel, go to: File > Options > Current Location and delete the VBAProject file from: C:\Users\[YourUser]\AppData\Roaming\Microsoft\Excel\EXCEL.EXE\VBAProject.7 (Replace `[YourUser]` with your username).
  2. Reinstall the Office application with default settings. This fixes corrupted object models but may remove add-ins or macros.
  3. Use a different macro recorder:
    1. In Excel, press Alt+F8, click "Record Macro," and let it run a simple task (e.g., copy/paste).
    2. If this works, your original code has a logic error (e.g., incorrect variable assignments).
    3. If it fails, the issue is in your runtime environment, like a missing sheet or library.
  4. Check for circular references:
    1. If two modules call each other, VBA may raise error 5. Use the Call Stack window (View > Immediate Window > type `Call Stack` and press Enter) to trace execution.
    2. Rename problematic procedures or restructure your code to avoid recursion.

Common Mistakes

  • Assuming objects exist at runtime: Code like `Sheets("MissingSheet").Activate` will fail if the sheet doesn’t exist. Always validate:
    If Worksheets("Sheet1").Exists Then
      Worksheets("Sheet1").Activate
    End If
  • Overloading procedure names: Naming a sub "MyFunction" conflicts with Excel/Word’s `Function` keyword for formulas. Use `Sub MyFunction()` instead.
  • Ignoring type mismatches: Passing the wrong data type (e.g., `String` where `Integer` is expected) can cause error 5. Use `IsNumeric` checks or explicit type casting:
    If IsNumeric(CellValue) Then
      MsgBox "Valid number"
    End If
  • Using incompatible Office versions: A procedure like `Worksheets.Add` works in Excel 2013 but may fail in older versions. Check the VBA documentation for your Office build.

Tips

  • Use the Object Browser (Ctrl+R) to see valid methods for an object (e.g., right-click `ThisWorkbook` in VBA to see available properties/methods).
  • Enable debugging symbols for Office:
    1. In Excel, go to: Tools > Options > General > Debugging and check "Display a message when Allocated Memory is Overridden."
    2. This catches invalid object references early.
  • Test on a clean workbook: Create a new Excel file, paste your macro, and test. This isolates whether the issue is in your code or your original project’s environment.
  • Avoid `On Error Resume Next` for error 5—it hides the root cause. Instead, catch specific errors:
    On Error GoTo ErrorHandler
      ' Your code here
    ErrorHandler:
      MsgBox "Error: " & Err.Description
  • Use `Dim` instead of `Declare` for simple cases. If you’re calling a function from a COM library, use `Declare` with the correct signature, but avoid it for basic Office automation.

Troubleshooting

  1. Error occurs only in specific files: The issue is likely in a custom module or add-in. Test the code in a blank workbook first.
  2. Error after updating Office: Corrupted object models may need a reinstall. Restart your computer after the update and verify no background Office processes are running.
  3. Error when opening the VBA editor (not just running macros):
    1. Close all Office instances.
    2. Run Excel as Administrator (right-click > Run as Administrator).
    3. Try opening the VBA editor again.
  4. Error in Access with DAO.Recordset: Ensure your connection string is valid. Test with a simple query:
    Dim rs As DAO.Recordset
    Set rs = CurrentDb.OpenRecordset("SELECT * FROM Table1")
    If rs.EOF Then MsgBox "Empty table"
  5. Error in Word with Selection objects: Word’s Selection object is fragile. Use `ActiveDocument.Content` or `ActiveDocument.Range` instead:
    ActiveDocument.Range.Text = "Hello"
  6. Error after installing a third-party library: Uninstall the library, restart, then re-install. Check the library’s documentation for required dependencies.

Frequently Asked Questions

Why does my macro work in the recorder but not when I type it manually?

Macros recorded in Excel/Word often use default values (e.g., `ActiveSheet` or `ActiveWindow`) that may not exist when you run them manually. Replace placeholders with explicit references, like `Sheets("Sheet1")` instead of `Sheets(1)`.

How do I fix an error like "Type Mismatch" that triggers error 5?

Error 5 can mask type mismatches. Use `TypeName` checks or explicit casting. Example:

If IsNumeric(Cell.Value) Then
  Cell.Value = CInt(Cell.Value) * 2
Else
  MsgBox "Not a number!"
End If

My VBA code works in one workbook but not another—why?

The issue is likely in environment differences: active sheet, used ranges, or COM add-ins. Test each workbook in isolation. If it’s a specific sheet, validate its existence:

If Worksheets("Data").Range("A1").Value <> "" Then
  ' Proceed
Else
  MsgBox "Sheet not found!"
End If

Can error 5 be caused by a corrupted VBA project file?

Yes. Corruption can make object references point to invalid locations. Delete the `.vbaProject` file (as described earlier) and reopen the workbook—this resets references without deleting your macros.

What should I do if error 5 happens only in Access?

Check your DAO/ADO connections. Ensure the table/recordset exists and has the correct schema. Example:

Dim rs As DAO.Recordset
On Error Resume Next
Set rs = CurrentDb.OpenRecordset("SELECT * FROM Customers")
If Err.Number <> 0 Then
  MsgBox "Table not found: " & Err.Description
Else
  rs.Close
End If

How do I debug a procedure name that’s causing error 5?

Use the Object Browser (Ctrl+R) to list methods for a specific object (e.g., `ThisWorkbook`). Compare your code’s procedure name with the valid list. If unsure, replace the call with a known valid method (e.g., `Worksheets(1).Activate` instead of an undefined `MyProcedure`).