run macro at opening of workbook in Excel

Q138. I want to automatically run an initialization procedure whenever I open my WorkBook so that it is ready for use - how can I achieve that?

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 excel run macro at opening of workbook

 

You can find similar Excel Questions and Answer hereunder

1) How can I avoid updates to cell values during macro execution?

2) Vba code to password protect workbook in Excel

3) 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?

4) How can I add a Timestamp after macro execution?

5) How can I save a WorkSheet as a new WorkBook using VBA?

6) How can I make my macro wait for 5 secs before executing the next command?

7) Calling a macro from another workbook in Excel

8) I want to automatically run a procedure whenever I close my WorkBook so that it is in a known state - how can I achieve that?

9) I have a very complex macro that runs for a long time - is there any way I can set up an audible alarm indicating macro run is over?

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

11) I frequently use a Macro - is there a way to quickly access the Macro using a Keyboard Shortcut?

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

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

14) How can I quickly navigate to a frequently used field in a voluminous worbook?

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