The VBA runtime error 98—"A property or method call cannot include a reference"—occurs when your VBA code attempts to access an invalid object, reference, or property that doesn’t exist in the current scope. This error pops up in Excel, Word, or other Office macros when functions like `Range`, `Worksheet`, or `ActiveSheet` reference a missing object, or when you try to call a method on a null reference. The fix depends on whether the issue stems from incorrect object qualification, missing references, or dynamic object handling. If you’re stuck here, the solution is to audit your code’s object references, ensure proper dependency initialization, and use conditional checks to avoid null values.

What You'll Need

  • A working copy of Microsoft Office (VBA Editor or IDE)
  • Your project file (Excel workbook, Word document, etc.)
  • An understanding of VBA object models (or basic debugging tools)

Step-by-Step

  1. Identify the problematic line Run your macro, pause execution (F8), and land on the error line. Right-click the line in the Code Editor and select "Go to Definition" (or "Go to Declaration") to verify the object being referenced. For example, if the error occurs at `Range("A1").Value = 1`, check if the sheet or workbook exists at runtime.
  2. Check object qualification If the error involves `Range`, `Worksheet`, or `Application`, ensure the object is properly qualified. For instance:
        ' Wrong (may reference a null object)
        Range("A1").Value = 1
        ' Correct (explicitly targets the active sheet)
        ActiveSheet.Range("A1").Value = 1
        
    If the object is dynamic (e.g., loaded after initialization), use conditional checks:
        If Not Worksheets("Sheet1") Is Nothing Then
            Worksheets("Sheet1").Range("A1").Value = 1
        End If
        
  3. Verify object existence For missing sheets, workbooks, or controls, declare an error message or skip the operation. Example for a missing sheet:
        On Error Resume Next
        Dim ws As Worksheet
        Set ws = ThisWorkbook.Worksheets("MissingSheet")
        On Error GoTo 0
        If ws Is Nothing Then MsgBox "Sheet not found!", vbExclamation
        Else ws.Range("A1") = "Data"
        End If
        
  4. Use `Is Nothing` checks For methods like `SendKeys` or `ActiveWindow`, wrap calls in null checks:
        If Not Application.IsNull(ActiveWindow) Then
            ActiveWindow.Activate
        End If
        
  5. Debug dynamic objects If objects (e.g., shapes, charts) are created programmatically, ensure references are set correctly. For example:
        Dim myShape As Shape
        On Error Resume Next
        Set myShape = ActiveSheet.Shapes("Box1")
        On Error GoTo 0
        If myShape Is Nothing Then
            Set myShape = ActiveSheet.Shapes.AddShape(1, 100, 100, 50, 50)
            myShape.Name = "Box1"
        End If
        

Common Mistakes

  • Assuming `ActiveSheet` or `ActiveWorkbook` exists These objects can be null if no sheets/workbooks are open. Always qualify with explicit references or use `If Not ActiveSheet Is Nothing Then ...`.
  • Ignoring case sensitivity In VBA, `Worksheets("Sheet")` and `Worksheets("SHEETS")` are different. Check object names are lowercase unless case-insensitive modes are enabled.
  • Overlooking `Option Explicit` Missing explicit declarations (e.g., `Dim ws As Worksheet`) can cause implicit types to evaluate to null, triggering the error. Enable `Option Explicit` in the VBA Editor to catch undeclared variables.
  • Using `SendKeys` without checks The `Application` object’s `SendKeys` method can fail if the active window isn’t focused. Always verify focus first.
  • Static references to deleted objects If a sheet or control is deleted post-declaration, a static reference will keep referencing the old object until reloaded, causing runtime errors.

Tips

  • Use `Err.Clear` and `Err.Number` Wrap critical sections in error handling to catch reference errors early:
        On Error Resume Next
        ' Risky code here
        On Error GoTo 0
        If Err.Number <> 0 Then
            MsgBox "Error: " & Err.Description
            Exit Sub
        End If
        
  • Test with minimal code Isolate the problematic section by creating a new macro with just the line causing the error. Gradually add lines until the error reappears.
  • Avoid global variables Global variables (declared outside functions) may persist to null states. Use local variables or pass objects explicitly.
  • Check for typos A minor typo in a sheet name (e.g., `"Sheet1"` vs `"Sheet12"`) can trigger the error. Use wildcards or loops for dynamic sheets:
        For Each ws In ThisWorkbook.Worksheets
            If ws.Name Like "Sheet*" Then ws.Range("A1") = "Valid"
        Next
        

Troubleshooting

  1. If the error persists after qualification checks Step through the macro with debug mode (F8) to trace where the object becomes null. Add `Debug.Print ws Is Nothing` at key steps to detect nulls during execution.
  2. For Excel: Check for hidden sheets or frozen panes A frozen pane or hidden sheet might cause `ActiveSheet` to behave unexpectedly. Unfreeze panes or reveal hidden sheets via `Sheets.Visible = True`.
  3. Reinitialize objects when dependencies change If the workbook or worksheet is resized/destroyed (e.g., by another macro), reset references in a `Workbook_Open` or `Worksheet_Activate` event handler.
  4. Test in a fresh copy Recreate the macro in a new workbook to rule out corruption in the original file (e.g., cross-references or hidden errors).

Frequently Asked Questions

Why does my VBA code work in design mode but fails at runtime?

It likely references objects that don’t exist at runtime. In design mode, objects like `ActiveSheet` or `ActiveWindow` are implicitly available, but at runtime, they depend on which sheet/window is active. Always qualify objects (e.g., `Worksheets("Sheet1")`) or use null checks.

How do I fix a runtime error when calling a method on `Application` (e.g., `Application.Quit`)?

Wrap the call in error handling:
On Error Resume Next
Application.Quit
On Error GoTo 0
If Err.Number <> 0 Then MsgBox "Failed to quit!"
Also verify the application isn’t already closed or locked.

What if my error occurs when looping through sheets?

Use conditional checks for each sheet:
For Each ws In ThisWorkbook.Worksheets
    If ws.Name = "ValidSheet" Then ws.Range("A1") = 1
Next
If the sheet name isn’t exact, use wildcards or enumerate only visible sheets:
For Each ws In ThisWorkbook.Worksheets
    If ws.Visible Then ws.Range("A1") = 1
Next

Can I use `SendKeys` with any window?

No. The method only works on the active window. Check focus first:
If Not Application.ActiveWindow Is Nothing Then
    Application.ActiveWindow.Activate
    Application.SendKeys "{TAB}"
End If

Why does my code fail when accessing a shape's `TextFrame` property?

If the shape is deleted or no longer exists, `TextFrame` will throw an error. Use null checks:
If Not myShape Is Nothing And Not myShape.TextFrame Is Nothing Then
    myShape.TextFrame.TextRange.Text = "Hello"
End If

Is there a way to debug dynamic object references?

Use `Debug.Print` to log object references:
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
Debug.Print "Sheet reference: " & ws.Name & ", Is Nothing: " & (ws Is Nothing)
Also, enable the Immediate Window (Ctrl+G) to log outputs interactively.