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 offEnableEvents. - Rare/Serious cause: Circular procedure dependencies (
Sub AcallsSub B, which callsSub C, which callsSub 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:
- Runtime Error 28 (Out of stack space): Means too many active procedure calls are nested in memory at the same time. It is a logic issue in procedure management, not a file size issue.
- Runtime Error 7 (Out of memory): Means system RAM heap capacity is exhausted, usually from loading massive arrays or millions of long text strings, see Runtime Error 7: Out of memory (Large array handling).
- Runtime Error 6 (Overflow): Means a single variable holds a numeric value that exceeds its declared bit limit, such as storing 40,000 in a 16-bit
Integervariable, see Runtime Error 6: Overflow (Variable value exceeds the Integer limit—use Long).
What To Do Right Now
- Press Ctrl + Break (or Esc) repeatedly if Excel is frozen in a macro execution loop.
- Click Reset (the square stop icon in the VBA toolbar) to clear all call frames from memory.
- If the crash occurred inside an event procedure like
Worksheet_Change, open the Visual Basic Editor Immediate Window (Ctrl + G) and typeApplication.EnableEvents = Truefollowed by Enter to re-enable worksheet events, see How to use the Immediate Window to debug variable values in real-time. - 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.EnableEventsorApplication.ScreenUpdatingremain 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:
- 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.
- Audit Event Procedures: Verify that every
Worksheet_Change,Worksheet_Calculate, orWorkbook_SheetChangeroutine contains explicitApplication.EnableEvents = Falseguards before modifying any cell ranges, see Events Debugging: Why EnableEvents = False is necessary to prevent infinite loops. - 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 - Trace Indirect References: Check complex multi-module macros to ensure utility functions do not invoke parent subroutines higher up the execution chain.
Symptom Escalators
- If screen updating or calculation modes remain locked frozen after breaking out of a recursive loop, see Building a “Reset” macro to fix Excel settings after a crash (Calculation, ScreenUpdating).
- If unexpected zero-value errors occur inside mathematical recursive functions after fixing loop bounds, see Runtime Error 11: Division by zero in VBA calculations.
- If object variables fail to initialize inside recursive procedures, review Runtime Error 91: Object variable or With block variable not set (The Set keyword trap).
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.