My brain has already left for the weekend.
Please tell me what I need to call a macro to have it run automatically when I open a certain workbook.
I thought it was Auto_Open, but nothing happens.
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » Macro to run when w/b opens (Excel ’97)
In 97+ best to use the Private Sub Workbook_Open() event at the Workbook level. In the VBE Project Explorer window (left side), open the Microsoft Excel Objects for the workbook, double click the Workbook Object, in the code pane Object drop-down (upper left) select Workbook and (usually it will be created for you, but if not) from the Procedure drop-down (upper right) select Open. See also this thread.
Workbook_Open is the way to do this, but there is a risk associated with using it as it relies on events being enabled. In practice, you can’t guarantee that another Workbook/Add-In has left events enabled.
For this reason, I always put an Auto_Open into my Excel projects, make Workbook_Open public, and call Workbook_Open from the Auto_Open. I also make sure Workbook_Open sets a flag (Class scope Boolean) to prevent it from running twice.
Some may consider this overkill, but in a situation where you really need to make sure your Workbook_Open code runs, and you have no control over other VBA that the users run in other Workbooks or Add-Ins then it is, unfortunately, necessary.
I like this idea as occasionally events have gotten disabled and I wasted too much time figuring out why my program isn’t running. I’ve never used Auto_Open as I started VBA in xl97. Where does it go? I tried adding it in ThisWorkbook but it didn’t run (yes it was public).
public sub Auto_Open debug.print "in Auto_Open" end sub
I then did the same debug.print statement in Workbook_open but when I then saved,closed, re-opened the WB I only saw the print message for WB open not Auto_Open. I then put Auto_Open in a Module but it didn’t get fired first either.
Deb
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.
Notifications