Most Valuable Professional


View Jan Karel Pieterse's profile on LinkedIn subscribe to rss feed
Subscribe in a reader

Subscribe to our mailing list

* indicates required

Audit !!!

Check out our RefTreeAnalyser
the ultimate Excel formula auditing tool.

Trainings

Excel VBA Masterclass (English)
Excel VBA for Financials (Dutch)

Third party tools

Speed up your file

FastExcel
The best tool to optimise your Excel model!

Repair your file

Stellar Phoenix Excel Repair
Best tool to repair corrupt Excel sheets and objects
Home > English site > Articles > Prevent Open Event
Deze pagina in het Nederlands

Preventing auto_open and Workbook_Open events from running

Introduction

As a fulltime developer I oftentimes open files containing VBA and want to be able to run code. At the same time I sometimes want to prevent Workbook_Open event code or an Auto_Open macro from running. This little article shows you how to achieve that.

Why would you want to do this? Typically, I use this on my own files in which I am still developing. The Open event might contain code that takes a while to run, or configures the project in a way which I don't want to happen when I start working with the file.

If the file you opened using the methods described below contains other event handlers, note that these event handlers will remain disabled until you start a macro manually (by clicking a command button or a menu entry) or if a User Defined function in your code has been called.

I do not recommend using this method to open files from sources you do not know or trust. The VBA code can contain events which will eventually run and possibly cause trouble.

Excel 2010, 2013, 2016

Macro security set to low or trusted document

If you have set your macro security to "Enable All Macros" or you have already set the document to trusted or the document is in a trusted folder, click File, Open, select your file and hold down the shift key when you click the Open button:

Selecting the file from the File, Open dialog

Fig. 1: Selecting the file from the File, Open dialog (Excel 2010, 2013, 2016)

Of course your file might be listed in the Most Recently Used files (MRU) list.  In that case, hold shift when you click the file in the list:

Clicking the file on the MRU

Fig. 2: Clicking the file on the MRU (Excel 2010)

Doing so will prevent the Workbook_Open event from firing and the Auto_Open macro from running.

Macro security set to prompt

If your document is not in a trusted folder, has never been set to be trusted and macro security is set to something other than enable all macros to run, you can repeat what I've shown above.

Normally, if you open such a document, Excel will show the security bar. In this situation however, due to you holding down shift, Excel shows this window:

Enable macros dialog

fig 3: Enable macros dialog (Excel 2010)

Because you held down the shift button when you clicked the file in the MRU or when you clicked the Open button, you can now just click "Enable macros" and no Automacros will run.

Excel 2007 and up

Macro security set to low or trusted document

Note that this seems to fail if the trusted folder is on a network share!

If you have set your macro security to "Enable All Macros" or you have already set the document to trusted or the document is in a trusted folder, click File, Open, select your file and hold down the shift key when you click the Open button:

Selecting the file from the File, Open dialog

Fig. 4: Selecting the file from the File, Open dialog (Excel 2007 and up)

Of course your file might be listed in the Most Recently Used files (MRU) list.  In that case, hold shift when you click the file in the list:

Clicking the file on the MRU

Fig. 2: Clicking the file on the MRU (Excel 2007 and  up)

Doing so will prevent the Workbook_Open event from firing and the Auto_Open macro from running.

Macro security set to prompt

If your document is not in a trusted folder, has never been set to be trusted and macro security is set to something other than enable all macros to run, you can repeat what I've shown above.

Normally, if you open such a document, Excel will show the security bar. In this situation however, due to you holding down shift, Excel shows this window:

Enable macros dialog

fig 3: Enable macros dialog (Excel 2007 and up)

Because you held down the shift button when you clicked the file in the MRU or when you clicked the Open button, you can now just click "Enable macros" and no Automacros will run. 

Excel 97, 2000, XP, 2003

Macro security set to low or trusted document

In case your macro security is set to low, or your VBA code is signed and you have set the publisher as trusted, you must hold down the shift key when you click the Open button on the File, Open dialog:

Opening the file from the File, Open dialog

Fig. 7: Opening the file from the File, Open dialog (Excel 97-2003)

Of course your file might be listed in the Most Recently Used files (MRU) list.  In that case, hold shift when you click the file in the list:

Clicking the file on the MRU

Fig. 8: Clicking the file on the MRU (Excel 97-2003)

Doing so will prevent the Workbook_Open event from firing and the Auto_Open macro from running.

Macro security set to medium or higher

If your macro security is set to al least "Medium" and you have never set its publisher to trusted (for a signed macro) you can forget about holding shift when opening the file or clicking the file on the MRU list. Instead, you must hold shift when you enable macros:

Hold shift when you press Enable macros

Fig.9: Hold shift when you press Enable macros

Doing so will prevent the Workbook_Open event from firing and the Auto_Open macro from running.

 


Comments

Showing last 8 comments of 85 in total (Show All Comments):

 


Comment by: Himanshu (5/3/2016 6:17:12 PM)

Hello,

I have an excel workbook which is macro enabled.

I believe there is something wrong with its coding, due to this file hangs while opening. For the last two days it is not opening at all, I can only see a blank screen and rotating cursor. It is also returning a message that "Excel is not responding". This workbook is important for me. Please can you help me to open this file. if the issue can't be resolve, please suggest me a way so that I can at least copy my macro coding, it will help me to save my efforts.

Thanks
Himanshu

 


Comment by: Jan Karel Pieterse (5/4/2016 2:34:51 PM)

Hi Himanshu,

Have you seen this page perhaps: http://www.jkp-ads.com/Articles/CorruptFiles.asp

 


Comment by: Arjen (5/8/2016 6:52:47 AM)

Hello Jan,
my question is also slightly of the topic but i hope you can answer this.

I have made an auto_open file (xlsm) and set macro security to “Enable all macros”.
If I open this file in the normal explorer or via another VBA command “ShellExecute”, excel will open with an empty screen. The macro will not be executed…?
Also a shortcut on my desktop screen results in a nothing happens event???

The only way I can start the program including its macro event is that I have to open the MRU (most recently used files) and click once on the name…..
PC is windows 7 and Excel version is 2016

By the way, I have tested the same file on a windows 8.1 and Excel 2013 and it will work normally.
Hope you can help,

Thanks in advance, Arjen

 


Comment by: Jan Karel Pieterse (5/11/2016 3:57:23 PM)

Hi Arjen,

Have a look at this article, it may contain a solution for this problem: www.jkp-ads.com/articles/startupproblems.asp

 


Comment by: Leon (6/3/2016 12:04:01 PM)

one little note to add on this topic - holding shift while opening the file only works when opening in Excel.

I don't think there's a way to open an xlsm file without the macro running if you open it in My Computer, for example.

If I'm wrong, I'd welcome a solution..

thanks, Leon

 


Comment by: vAPL (3/18/2017 7:08:16 AM)

want to stop macro run automatically in open sheets except sheet for which macro is written

 


Comment by: Jan Karel Pieterse (3/20/2017 10:05:11 AM)

Hi vAPL,

I guess you could start each event with a test for which sheet is active and act accordingly:

If Activesheet.Name <> "TheSheetForThisMacro" Then Exit Sub

 


Comment by: Rick Walton (4/6/2017 10:54:21 AM)

If you alter the workbook.open event code to search the open workbooks for a specific named item (like editmode.xlsx), and if found then 'end', otherwise continue on with running the code. If you want to open without executing the code just make sure you have the killer workbook open prior to opening the macro enabled workbook.

 


Have a question, comment or suggestion? Then please use this form.

If your question is not directly related to this web page, but rather a more general "How do I do this" Excel question, then I advise you to ask your question here: www.eileenslounge.com.

Please enter your name (required):

Your e-mail address (optional but if you want me to respond it helps!; will not be shown, nor be used to send you unsolicited information):

Your request or comment:

To post VBA code in your comment, use [VB] tags, like this: [VB]Code goes here[/VB].