Runtime Error 28: “Out of stack space,” explained and fixed

Error 28, “Out of stack space”, means VBA filled the call stack, the memory that tracks who called whom. In practice it is recursion that never bottoms out: a function that calls itself with unchanged arguments, or a pair of event procedures (a Change handler that changes the cell, which fires Change again) chasing each other until the stack gives out. It is a logic bug, and the data usually has nothing to do with it.

For people who just run the program

  1. Note the action that starts the freeze. Error 28 typically arrives after a hang: the application churns, then reports the error. Typing in one cell, opening one form, clicking one button are the usual starts.
  2. Break out with Ctrl+Break if the application is still looping. That stops the macro without killing the application, so unsaved work elsewhere survives.
  3. Check whether it happens in a fresh blank file. If yes, the code itself loops; if only in one file, a stored value in that file is feeding the loop its endless condition.
  4. Report the triggering action, not just the number. “Happens when I edit the totals cell” points straight at an event loop.

For people who write the code

  1. Look for event cascades first in Office code. A Worksheet_Change handler that writes to the sheet re-fires itself. Disable events around your own writes:
  2. Private Sub Worksheet_Change(ByVal Target As Range)
        Application.EnableEvents = False   ' stop my own writes re-firing Change
        Target.Offset(0, 1).Value = Target.Value * 2
        Application.EnableEvents = True
    End Sub
  3. Verify the recursion's exit condition and its progress. Print the arguments at each level. If the values are not moving toward the base case, the condition or the step is wrong, not the stack size.
  4. Check property procedures. A Property Let or Get that refers to itself by name instead of its backing variable recurses on every access and is a classic hidden cause.
  5. Convert deep recursion to an explicit loop. Where recursion is legitimate but deep (tree walks over huge structures), an iterative version with a Collection as the work list has no stack limit worth mentioning.
The stack cannot be enlarged. There is no setting that gives VBA a bigger call stack. Any fix that “makes room” instead of stopping the runaway calls only delays error 28.

If it still happens

If no self-calls are visible, suspect mutual recursion through the host: your handler calls a built-in command, the command fires another event, that event calls your handler. The call stack window (Ctrl+L in the editor) shows the repeating pattern of procedure names when you break in.

Common questions

Why does the application freeze before error 28 appears? The loop runs at full speed consuming stack; the error can only be raised when the stack is completely exhausted, which can take a noticeable number of repetitions, all of which look like a hang.

Is error 28 caused by large data? Rarely directly. Large structures deepen legitimate recursion, but a stack overflow almost always means calls are repeating without progress, not that the data is big.

Related errors

Runtime Error 7 · Runtime Error 16 · Runtime Error 6