Home > Enterprise >  Calling / faking a "Workbook_Open" event in VBA in Excel?
Calling / faking a "Workbook_Open" event in VBA in Excel?

Time:02-18

So I have a VBA project that it would be useful to be able to restart as though it had just been opened. However, I don't want to actually restart the workbook. I have tried to close then reopen the file to trigger the "Workbook_Open" event but Excel keeps randomly opening the project in a new instance.

So I'm wondering if it is possible for me to call/fake the Workbook_Open event?

Thanks.

CodePudding user response:

You can just call the Workbook_Open event-handler directly. It will not in any sense "re-open" the workbook, but as event-handlers are just subroutines, you can of course call them like any other subroutine.

However, this is somewhat "tricky" and ill-advised because later programmers may add things that should be done at Workbook_Open but that have nothing to do with your specific need so, as @sous2817 noted in the comments, a better way to do this is to move the Workbook_Open code to its own subroutine and then have both your code and Workbook_Open call it there.

CodePudding user response:

Even though the event is private, ThisWorkbook.Workbook_Open will run the Workbook_Open event.

CodePudding user response:

I believe these event handlers, if declared public, are ordinary subs. You should be able to call them from your main module with Worksheet("Name").Workbook_Open. If that failed for some reason (I cannot fathom), just empty Workbook_Open into public sub myCoolBeans() and call myCoolBeans() from Workbook_Open and where-ever.

  • Related