RunAutoMacros Method of Workbook Object VBA

In VBA ‘RunAutoMacros’ Workbook method is used to runs the ‘xlRunAutoMacros’ which is attached to the workbook. Where xlRunAutoMacros’ can be one of these: xlAutoActivate or xlAutoDeactivate or xlAutoOpen or xlAutoClose. We can write these macros in the standard module. If auto run macros are available in workbook events then it will fire the auto run macros in the standard module before running in workbook events.



50+ Project Management Templates Pack
Excel PowerPoint Word

Advanced Project Plan & Portfolio Template
Excel Template

Business Presentations Templates Pack
PowerPoint Slides

20+ Excel Project Management Pack
Excel Templates

20+ PowerPoint Project Management Pack
PowerPoint Templates

10+ MS Word Project Management Pack
Word Templates

Why we need RunAutoMacros Workbook using VBA?

If we want to run macros automatically in certain time we use RunAutoMacros method of Workbook object.

VBA RunAutoMacros Workbook – Syntax

Here is the syntax to RunAutoMacros workbook method using VBA.

Workbooks(“Your Workbook Name”).RunAutoMacros(Which As XlRunAutoMacro)

Where ‘Which’ is the required parameter. ‘Which’ can be one of the below.

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

In the above syntax we are using ‘RunAutoMacros‘ method of workbook object to run auto macros in the standard module.

VBA RunAutoMacros Workbook: Example 1

Please find the below example, It will show you how to run auto macros in the standard module.
Standard Module:

Private Sub Auto_Open()
MsgBox "Auto Open Event"
End Sub

Workbook Event:

Private Sub Workbook_Open()
MsgBox "Workbook Open Event"
End Sub

Explanation: You can write procedures in the standard module and then call those macros in the workbook events instead of writing it in Workbook event. Its use to navigate the code in the standard module while debugging the code.
Note:If we have written both the above macros in the same workbook, while running macro it will fire the AutoRun macro in the standard module first and then it will run workbook event.

VBA RunAutoMacros Workbook – Instructions

Please follow the below step by step instructions to execute the above mentioned VBA macros or codes:

  1. Open an Excel Workbook
  2. Press Alt+F11 to Open VBA Editor
  3. Insert a Module from Insert Menu
  4. Copy the above code for activating a range and Paste in the code window(VBA Editor)
  5. Save the file as macro enabled workbook
  6. Press ‘F5’ to run it or Keep Pressing ‘F8’ to debug the code line by line.

Leave A Comment

Related pages

iserror formula excelexcel vba case statementmerge two columns in excel 2010vba code to open excel filemacro security excel 2010messagebox in vbaunprotect worksheet vbadefinition of row and column in ms excelloop in macro excelvba delete duplicatesmerge excel cellshow to calculate difference between dates in excelexcel vba workbooksas analytics tutorialexcel sheet delete shortcut keyvlookup for beginnersexcel vba read onlyexcel vba ucaseselect vba excelexcel macro remove duplicateshow to merge fields in excelworkbooks.activateexcel vba internet explorerapplication workbooksformula concatenate excelmeaning of vlookup and hlookupms excel 2007 formulas and functionsdim rs as recordsetsql in sasexcel activex controlmeaning of vlookup and hlookupexcel vba calculateexcel formula insert rowdata definition language commands in sqlhow to create database in excel sheetunlock worksheet exceloledb connection string sql server 2012writing vba codeusing excel functions in vbamsgbox access vbaddl dml dcl commands in sql serverexcel data from another workbookuserforms in excel 2010vba message box codehow to insert command button in excelscripting.filesystemobject vbaexcel 2010 column widthdelete rows in excel vbavba collectionsexcel vba open txt fileexcel macro loopsvlookup for dummies tutorialshortcut formulas in excelaccess vba excelhow to stop vba code runningvbscript code examplesexcel 2007 developer tab addsort in ascending order excelsimple vlookupmacro creation in excelvba floatexcel vba selectfso.deletefoldermicrosoft excel templates for project managementexcel spreadsheets for beginnerspopulate combobox excelhow to lock the excel sheetlookup command excelvba access tutorialexcel dashboard kpiconverting excel to csvexcel vba database examplesfind duplicate rows in excel 2007userform in vbahow to delete an excel sheetadd drop down list in excel 2007ppt schedule templateexcel vba isnumberfind duplicate values in excel