Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in


How to run VBA macro when open or close workbook?

In many cases we may use the VBA code to achieve the operations which Excel built-in functions not support. But have you ever tried to run the VBA macro in each time of opening or closing the workbook. In this article, I will tell you how to run the VBA code while opening or closing the workbook every time.

Run VBA code when close or open workbook

Tabbed browsing & editing multiple Excel workbooks/Word documents as Firefox, Chrome, Internet Explore 10!

You may be familiar to view multiple webpages in Firefox/Chrome/IE, and switch between them by clicking corresponding tabs easily. Here, Office Tab supports similar processing, which allow you to browse multiple Excel workbooks or Word documents in one Excel window or Word window, and easily switch between them by clicking their tabs. Click for free 45-day trial of Office Tab!

ot excel

Run VBA code when close or open workbook

1. Enable the workbook, press Alt + F11 keys to open the Microsoft Visual Basic for Applications window.

2. Double click ThisWorkbook in Project – VBAProject pane to open the ThisWorkbook (Code) window.
doc run code when open or close 1

2. In the code window, select Workbook from the left drop down list. Notice that, in default, the Open will be displayed in right drop down list, if not, change it to Open.
doc run code when open or close 2

3. Then copy your own code without the first and last line and paste between Private Sub Workbook_Open() and End Sub as below screenshot shown.
doc run code when open or close 3

4. Then click Save button to save the code, in the popping reminder dialog, click No.
doc run code when open or close 4

5 Then a Save As dialog pops out, save the file as Excel Macro-Enabled Workbook and select a folder to place the file.
doc run code when open or close 5

6. Click Save button. Now the code will run while the workbook is open.

If you want to run the code while closing workbook each time, select Deactiviate from the right drop down list in the Code window, and copy the code you will run and paste between Private Sub Workbook_Deactivate() and End Sub.

Easily Combine multiple sheets/Workbook into one Single sheet or Workbook

To combinne multiples sheets or workbooks into one sheet or workbook may be edious in Excel, but with the Combine function in Kutools for Excel, you can combine merge dozens of sheets/workbooks into one sheet or workbook, also, you can consolidate the sheets into one by several clicks only.  Click for 60 days free trial!
combine sheets
Kutools for Excel: with more than 200 handy Excel add-ins, free to try with no limitation in 60 days.

Recommended Productivity Tools for Excel

Kutools for Excel Helps You Always Finish Work Ahead of Time, and Stand Out From Crowd

  • More than 300 powerful advanced features, designed for 1500 work scenarios, increasing productivity by 70%, give you more time to take care of family and enjoy life.
  • No longer need memorizing formulas and VBA codes, give your brain a rest from now on.
  • Become an Excel expert in 3 minutes, Complicated and repeated operations can be done in seconds, 
  • Reduce thousands of keyboard & mouse operations every day, say goodbye to occupational diseases now.
  • 110,000 highly effective people and 300+ world-renowned companies' choice.
  • 60-day full features free trial. 60-day money back guarantees. 2 years of free upgrade and support.

Brings Tabbed Browsing and Editing to Microsoft Office, Far More Powerful Than The Browser's Tabs

  • Office Tab is designed for Word, Excel, PowerPoint and Other Office Applications: Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.

Be the first to comment.