Thursday, April 16, 2009

Macros in Excel : Auto Open Macro / Auto Run Macro

Many times we want certain task to be executed when you open a particular file. This can be achieved by auto open macro or auto run macro. This is the macro which executes itself when you open a excel file.

There are two methods to do this

Method 1. Auto open macro in VBA Project Module

In this case you add your macro code to VBA project module and it gets executed when you open the file. When you go to your VBA editor, you select a module as highlighted in the picture and copy following code to have auto run macro.

Private Sub Auto_Open()

MsgBox "This is auto open macro in Module"

End Sub

Method 2. Auto open macro in ThisWorkbook Section

In this method you add your code to Thisworkbook Section of your excel file. double click on highlighted potion in your VBA editor and paste following code

Private Sub Workbook_Open()

MsgBox "This is auto open macro in This workbook"

End Sub

You can replace the message box line with your code / action you want your macro to do. I have used this as example to keep my macro code short for better understanding.

Auto open Macro , Excel auto open macro , Auto run macro , Auto open excel , VBA excel workbook open


  1. how to get around enable content so the auto open macro will be enabled?

  2. how to get around enable content so the auto open macro will be enabled?

  3. Can I make an Auto_open macro conditional dependent upon a cell value contained within the spreadsheet?

    1. Of course. You can use "If .... Then .... Else", "Select Case" etc.

      If CONDITION 1 Then
      Your code
      Elseif CONDITION 2 Then
      Your other code
      Else Exit Sub

  4. can you call a "beforeclose macro" in the module too?

  5. I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. auto repair near me

  6. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work... text auto reply

  7. Albania is one of the best countries for real adventures into the nature . The country offer a lot of camping sites from South to the North and from East to the West . They are furnished with all the necessities in very economic prices, with very hospitable staff , and also with food services for the tourist that prefer visit Albania through their own camper vans or rent just a tent. Albania Camping

  8. This is a great inspiring article.I am pretty much pleased with your good work. luxury car rental with driver | limousine service dubai

  9. Always renew 10year . Don't be like the car dealer that they will only do 5year. They kill it . Like a cancer for the car in 5year. Top of the cost. I have renew all my car and bike 10year . I have keep a few car and bike and they run good. Depend how you take care of them. carcar

  10. Rent a car Islamabad is one of the most trustworthy and renowned name of the VIP car rental market place in Pakistan.
    We offer premium car rental service at affordable charges to keep your traveling need absolutely budget friendly.
    carcarshire a car

  11. Thank you for such valuable information.Keep sharing.
    Get the best bus and car rental service in Dubai.
    van for rent with driver

  12. It is vital to work with a roofer who is focused on client benefit and treating clients decently! limousine service dubai | luxury car rental with driver

  13. The article looks magnificent, but it would be beneficial if you can share more about the suchlike subjects in the future. Keep posting. limousine service Dubai~~luxury car rental with driver

  14. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!. Luxury Rent a Car with Driver & Limousine Ride Dubai

  15. It is my first visit to your blog, and I am very impressed with the articles that you serve. Give adequate knowledge for me. Thank you for sharing useful material. I will be back for the more great post. Nevada Fake ID

  16. Good work! Such a amazing and helpful post this is. I really really love it. I am just amazed. look this Dubai Airport Transport

  17. I think this web site has very superb composed subject material articles . Movers in Palm Jumeirah

  18. You've completed in excellent work. t suggest to my friends and personally. Dubai Airport Transport

  19. I really loved reading your blog. It was very well authored and easy to understand. Unlike other blogs I have read which are really not that good. Thanks a lot! Womens swimwear


Learn how to create Excel dashboards.
Yogesh Gupta's Excel Tips