Runtime Error 28: Out of stack space (Recursive loops gone wrong)

VBA Runtime Error 28 occurs when the execution memory buffer, known as the call stack, runs completely out of capacity due to endless procedure calls. This instantly halts code execution, leaving macros crashed and workbooks in potentially incomplete states. Addressing Error 28 requires identifying unbounded recursive procedure calls or cascading worksheet events that repeatedly invoke themselves without returning control to Excel.

Fast-Fix: The 45-Second Solution

Excel VBA Runtime Error 28: Out of stack space occurs when infinite recursive loops, deeply nested subroutines, or unmanaged event triggers consume Excel’s allocated execution memory. To fix it, ensure recursive functions include a definite termination condition (base case), disable cascading sheet events using Application.EnableEvents = False, and refactor overly deep nested calls into iterative loops.

Quick Risk Snapshot

  • Severity Tier: High (causes immediate macro abortion, potential application freezes, and partial write operations).
  • Is it safe to ignore? No. The code will crash every single time the recursive boundary or event loop is triggered.
  • Most common cause: Event handlers (Worksheet_Change, Workbook_Open) modifying cells without turning off EnableEvents.
  • Rare/Serious cause: Circular procedure dependencies (Sub A calls Sub B, which calls Sub C, which calls Sub A).

What Escalates the Risk

The risk of triggering Error 28 increases drastically when macros interact with dynamic workbook elements. When automatic calculation or worksheet events are active across large data tables, a single cell write inside an unsuppressed event procedure can generate thousands of sub-events in a fraction of a second.

Furthermore, if error-handling blocks (On Error GoTo) redirect execution back to the beginning of a failing procedure without resetting variable states, the macro enters a self-referencing crash loop that rapidly exhausts stack memory.

Common Confusion Fix

Runtime Error 28 is frequently confused with general system memory failures:

What To Do Right Now

  1. Press Ctrl + Break (or Esc) repeatedly if Excel is frozen in a macro execution loop.
  2. Click Reset (the square stop icon in the VBA toolbar) to clear all call frames from memory.
  3. If the crash occurred inside an event procedure like Worksheet_Change, open the Visual Basic Editor Immediate Window (Ctrl + G) and type Application.EnableEvents = True followed by Enter to re-enable worksheet events, see How to use the Immediate Window to debug variable values in real-time.
  4. Wrap any cell modification inside event macros with event-suppression flags: VBA Private Sub Worksheet_Change(ByVal Target As Range) On Error GoTo CleanExit Application.EnableEvents = False ' Your cell editing logic here Target.Value = UCase(Target.Value) CleanExit: Application.EnableEvents = True End Sub

Hard-Stop Triggers

Immediately halt and reset execution if:

  • The macro is stuck in a self-triggering loop that cannot be paused with Ctrl + Break, causing Excel’s title bar to read “Not Responding”.
  • The code was modifying central shared databases or master inventory tables when the stack overflow occurred.
  • Application.EnableEvents or Application.ScreenUpdating remain disabled globally, rendering Excel interface elements unresponsive across all open workbooks.

Professional Audit Path

To eliminate stack overflow risks across your macro projects, follow this standard inspection workflow:

  1. Inspect the Call Stack: When paused at a breakpoint during debugging, press Ctrl + L in the Visual Basic Editor to open the Call Stack dialog box. If you see the exact same procedure listed dozens of times, you have identified an active recursion loop.
  2. Audit Event Procedures: Verify that every Worksheet_Change, Worksheet_Calculate, or Workbook_SheetChange routine contains explicit Application.EnableEvents = False guards before modifying any cell ranges, see Events Debugging: Why EnableEvents = False is necessary to prevent infinite loops.
  3. Validate Recursive Exit Conditions: Ensure every recursive function includes an explicit base condition that executes before the self-referencing call: VBA Function Factorial(n As Long) As Long ' Base Condition (Prevents Error 28) If n <= 1 Then Factorial = 1 Else Factorial = n * Factorial(n - 1) ' Recursive Call End If End Function
  4. Trace Indirect References: Check complex multi-module macros to ensure utility functions do not invoke parent subroutines higher up the execution chain.

Symptom Escalators

Final Calculation

VBA Runtime Error 28 is a direct result of unmanaged program flow filling the call stack memory buffer. It is almost always caused by worksheet event macros editing cells without disabling Application.EnableEvents, or recursive routines missing a firm termination condition. By wrapping event code with event-suppression flags and verifying base exit conditions in recursive functions, you can prevent stack overflow crashes and keep your automation running smoothly.