Skip to main content

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


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.

Best Office Productivity Tools

🤖 Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functions
Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |   Round without Formula ...
Super Lookup: Multiple Criteria VLookup    Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup ....
Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List ....
Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ...
Featured Features: Grid Focus   |  Design View   |   Big Formula Bar    Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells    Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ...
Top 15 Toolsets12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more

Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time.  Click Here to Get The Feature You Need The Most...

Description


Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, 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!
Comments (1)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hello,

I have used this VBA code with some success. I find that when I am working on more than one workbook at a time, each time I switch the focus from the macro enabled workbook to another workbook, the macro is run. I don't want it to run until I actually manually "CLOSE" the file. Do you now how I can accomplish that?
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations