I am opening spreadsheets using VBA and a couple of the workbooks contain code which starts executing when Workbook_Open() is called.
How can I open the workbooks using VBA but stop the code automatically executing? I am only opening the workbooks to look at formulae in the sheet- I do not want any code execution.
Start Excel Application > Go to File > Recent > Doing this will prevent the Workbook_Open event from firing and the Auto_Open macro from running.
To run a workbook without triggering a startup macro, you need to open it from within Excel, rather than double-clicking the file in Windows. Open Excel, go to the File menu, click "Open" and locate your file. Hold down the "Shift" key while you click "Open," and continue holding it until the workbook finishes loading.
Steps to disable the VBA macroClick on Tools > Visual Basic for Application. From the VB Project window, select the desired module which contains the VBA code. Use the mouse (press and hold left-click button) to highlight all the code that you would like disabled.
Would you like to try disabling the Events before you open the workbook in VBA and then re-enabling them for the rest of the module? Try using something like this:
Application.EnableEvents = False 'disable Events
workbooks.Open "WORKBOOKPATH" 'open workbook in question
Application.EnableEvents = True 'enable Events
I don't know why this was not clearly mentioned in the other answers but I found Application.AutomationSecurity
to do exactly what was required. Basically
Application.AutomationSecurity = msoAutomationSecurityByUI
'This is the default behavior where each time it would ask me whether I want to enable or disable macros
Application.AutomationSecurity = msoAutomationSecurityForceDisable
'This would disable all macros in newly opened files
Application.AutomationSecurity = msoAutomationSecurityLow
'This would enable all macros in newly opened files
Even after the code is run the settings will not revert back to the default behavior so you need to change it again. Thus for this question
previousSecurity = Application.AutomationSecurity
Application.AutomationSecurity = msoAutomationSecurityForceDisable
' Your code
Application.AutomationSecurity = previousSecurity
Here another way to open with out the vba
Start Excel Application > Go to File > Recent >
Hold Shift key and double click to open -
Doing this will prevent the Workbook_Open
event from firing and the Auto_Open
macro from running.
Or hold shift key and double click to open the Workbook.
For
VBA
Work with Application.EnableEvents property (Excel)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With