Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug an Excel Auto-Open macro before executing it?

I'm being asked to fix an Excel workbook where macros aren't working properly. The workbook includes an Auto-Open macro that changes content, including hiding or removing worksheets, and once it has run through, a second run always crashes because it's trying to select a worksheet previously deleted.

I want to step through Auto-Open but here's the catch:
If I open the workbook and choose to disable macros the macros is not viewable.
If I open the workbook and choose NOT to disable macros Auto-Open runs before I can stop it.

How can I step through Auto-Open on its first run?

like image 338
Brian Lowe Avatar asked Feb 16 '26 11:02

Brian Lowe


2 Answers

Full answer trail is in the comments.

Macros are available for editing or debugging when disabled, but you need to use the right-click context menu in the expanded tree view of Microsoft Excel Objects to load the code into the edit panel (a single click shows an empty panel).

I put a "Stop" as the first command in the Auto-Open macro and saved the workbook. On re-opening the workbook I can choose to enable macros and let Auto-Open run, and jump straight into the debugger to step through on a first run.

like image 128
Brian Lowe Avatar answered Feb 19 '26 00:02

Brian Lowe


Look at Debug.Assert False

You can just insert this as your first line in the auto-open subroutine

Debug.Assert Condition will stop the code and prompt debug if Condition = False

like image 23
Filcuk Avatar answered Feb 19 '26 01:02

Filcuk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!