Error 28: Out of stack space

The stack is a the working memory of the computer and it expands and contracts based on the demands of the programs running on the computer.  If your VBA is too labor intensive it will cause the stack to overflow and throw error 28 out of stack space.   Its limits have been exceeded.

To correct this error

  1. Check that procedures are not nested too deeply.

  2. Make sure recursive procedures terminate properly.
  3. If local variables require more local variable space than is available, try declaring some variables at the module level. You can also declare all variables in the procedure static by preceding the PropertySub, or Functionkeyword with Static. Or you can use the Static statement to declare individual static variables within procedures.
  4. Redefine some of your fixed-length strings as variable-length strings, as fixed-length strings use more stack space than variable-length strings. You can also define the string at module level where it requires no stack space.
  5. Check the number of nested DoEvents function calls, by using the Calls dialog box to view which procedures are active on the stack.
  6. Make sure you did not cause an “event cascade” by triggering an event that calls an event procedure already on the stack. An event cascade is similar to an unterminated recursive procedure call, but it is less obvious, since the call is made by Visual Basic rather than an explicit call in the code. Use the Calls dialog box to view which procedures are active on the stack.
  • You have too many active FunctionSub, or Property procedure calls. Check thatprocedures aren’t nested too deeply. This is especially true with recursive procedures, that is, procedures that call themselves. Make sure recursive procedures terminate properly. Use the Calls dialog box to view which procedures are active (on the stack).
  • Your local variables require more local variable space than is available.

    Try declaring some variables at the module level instead. You can also declare all variables in the procedure static by preceding the PropertySub, or Function keyword with Static. Or you can use the Static statement to declare individual Static variables within procedures.

  • You have too many fixed-length strings. Fixed-length strings in a procedure are more quickly accessed, but use more stack space than variable-length strings, because the string data itself is placed on the stack. Try redefining some of your fixed-length strings as variable-length strings. When you declare variable-length strings in a procedure, only the string descriptor (not the data itself) is placed on the stack. You can also define the string at module level where it requires no stack space. Variables declared at module level are Public by default, so the string is visible to all procedures in the module.

  • You have too many nested DoEvents function calls. Use the Calls dialog box to view which procedures are active on the stack.
  • Your code triggered an event cascade. An event cascade is caused by triggering an event that calls an event procedure that’s already on the stack. An event cascade is similar to an unterminated recursive procedure call, but it’s less obvious, since the call is made by Visual Basic rather than by an explicit call in your code. Use the Calls dialog box to view which procedures are active (on the stack).