Run macro at opening of workbook in Excel

To do it in Excel, here is the answer:

  1. Option Explicit
  2. Private Sub WorkBook_Open()
  3. Call InitializeMyWorkBook
  4. End Sub

a) The above macro code needs to be included under "ThisWorkBook" as shown below. When the WorkBook is opened, "WorkBook_Open" procedure is run which in turn calls "InitializeMyWorkBook".

CAUTION: If the user holds down the "Shift" key or chooses to disable macros when the file is opened, this procedure will not run.

excel run macro at opening of workbook

  1. Option Explicit
  2. Public Sub InitializeMyWorkBook()
  3. 'Initialization code goes here.
  4. End Sub

b) The custom "InitializeMyWorkBook" macro code needs to be included under "Modules" as shown below.

excel run macro at opening of workbook

 

You can find similar Excel Questions and Answer hereunder

1) How do I assign a macro to a shape using VBA?

2) How can I activate a routine when there is a change in value of a cell?

3) How to do workbook protection with VBA in Excel

4) How can I prevent users from seeing / accessing my macro code?

5) How can I clear cell after activating a routine when there is a change in value of a cell?

6) I have a WorkBook that loads a form automatically when it is opened. In Excel, how can I suppress the form from loading on file open when required?

7) I frequently use a Macro - is there a way to quickly access the Macro in the Excel Ribbon?

8) Calling a macro from another workbook in Excel

9) Here we explain the thisWorkbook object and its functionalites

10) How can I execute an event at a predetermined time from my Macro?

 

Here the previous and next chapter