Skip to main content

Workbook.RunAutoMacros method

Runs the Auto_Open, Auto_Close, Auto_Activate, or Auto_Deactivate macro attached to the workbook. This method is included for backward compatibility. For new Visual Basic code, you should use the Open, Activate, and Deactivate events and the Close method instead of these macros.


expression.RunAutoMacros (Which)

expression A variable that represents a Workbook object.


Name Required/Optional Data type Description
Which Required XlRunAutoMacro Specifies the automatic macro to run.

XlRunAutoMacro enumeration

Specifies the automatic macro to run.

Name Value Description
xlAutoActivate 3 Auto_Activate macros
xlAutoClose 2 Auto_Close macros
xlAutoDeactivate 4 Auto_Deactivate macros
xlAutoOpen 1 Auto_Open macros


This example opens the workbook Analysis.xlsx and then runs its Auto_Open macro.

Workbooks.Open "ANALYSIS.XLSX" 
ActiveWorkbook.RunAutoMacros xlAutoOpen

This example runs the Auto_Close macro for the active workbook and then closes the workbook.

With ActiveWorkbook
    .RunAutoMacros xlAutoClose
End With

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>