Fix Excel VBA Worksheet_Change Stack Overflow When Updating Cells
Question details
Prevent a Worksheet_Change macro from causing an out-of-stack-space error while updating cells.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A worksheet event responds to changes in C27 and C31 and also checks B37 before writing to H38.
- Observed behavior
- After the B37 check and H38 update are added, the macro raises run-time error 28, Out of stack space. B37 is formula-driven, so its recalculated value may not trigger Worksheet_Change.
Open the workbook in Excel, press Alt+F11, and locate the affected worksheet's code module. Check which cells are edited directly, which contain formulas, and whether any relevant cells are merged.
Disable events while Worksheet_Change updates cells
Use this approach to stop writes such as the H38 update from recursively triggering Worksheet_Change.
Add the event guard to the existing Worksheet_Change procedure rather than creating a second procedure with the same name. Keep the existing business rules inside the guarded section.
Press Alt+F11, then double-click the affected sheet under Microsoft Excel Objects in the VBA Project pane.
In the existing Worksheet_Change procedure, use Intersect to check Target against the cells that should trigger the logic, such as C27, C31, and C35 if C35 is edited directly. Include B37 only if it can also be changed directly; formula recalculation alone does not fire Worksheet_Change.
Before the code that writes to H38 or other cells, set Application.EnableEvents = False. Add an error handler and a cleanup section that restores Application.EnableEvents = True on both normal and error exits.
Return to the worksheet and edit C27, C31, and any other intended input cells. Confirm that H38 updates as expected and that later edits still trigger the event.

Handle formula-driven changes to B37
Use this approach when the action must run after B37 changes because its formula recalculates.
Work with Excel spreadsheets in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for common Excel formats, making it easier to move everyday spreadsheet work. It does not fix an Excel VBA event error, and macro compatibility depends on the WPS version and configuration.

Frequently Asked Questions
Why does writing to H38 cause error 28?
A write made inside Worksheet_Change can trigger the same event again, creating repeated calls until Excel runs out of stack space. Disable events before the write and restore them in a cleanup section.
Why doesn't Worksheet_Change run when B37's formula result changes?
Formula recalculation does not itself fire Worksheet_Change. Monitor a directly edited precedent such as C35 when appropriate, or use Worksheet_Calculate to detect a changed result.
How do I turn Excel events back on after stopping a macro?
In the VBA editor, press Ctrl+G to open the Immediate window, enter Application.EnableEvents = True, and press Enter.
Can merged cells affect the event logic?
Yes. Merged cells can affect which range is reported or where a value is stored. Check Home > Merge & Center and use the merged range's top-left cell when appropriate.




