Automatically Run an Excel Macro

Auto run macro excel… Do you want to run an excel macros automatically when the file is open or close?

Microsoft Excel provide us the opportunity to run a macro automatically every time we open or close the excel workbook file, using the event Workbook_Open and Workbook_BeforeClose.

In the Microsoft Visual Basic Editor page, in the ThisWorkbook module, we simply create two new sub procedure called Workbook_Open and Workbook_BeforeClose. In both sub procedure, we call the method that we want to run automatically when the workbook is open or closed.

Private Sub Workbook_Open ()

    Call onFileOpenMacro

End Sub
Private Sub Workbook_BeforeClose (Cancel As Boolean)

    Call onFileCloseMacro

End Sub

In this example, when the workbook is opened, we will automatically run macros called onFileOpenMacro, we also automatically run macros called onFileCloseMacro just before the excel workbook is closed.

Related Entries

  • Schedules to Refresh Data Automatically using VBA Macro

    I once asked by Joshuacht about how to make a macro running every 15 minutes. Let say because every 15 minutes we have to refresh or reload the data in the Workbook. Below is a sample of how we can do this in Microsoft Excel *just an outline*. The logic is simple, when we open the...

  • Changing Microsoft Excel Status Bar

    Maybe for some reason we want to change our Microsoft Excel Status Bar, then this simple vba function will do the work for us. The end result will look like this:Ok, to do it, put this following code in ThisWorkbook codePrivate Sub Workbook_Open() 'when workbook opened, change the status bar ...

  • How To Open Excel File Using Macro

    Imagine this condition; We are in the middle of doing something using Excel VBA macro, then we want the macro to automatically show the File Open dialog box to make us (user) able to choose another Excel file to be opened by Excel. Got the picture? Below is vba procedure that do exactly like that, show...

  • Check If Excel Workbook is Already Open or Not

    In my case, I work a lot with transferring data from current Excel Workbook to another Excel Workbook, and to be able to do that, of course I need to make sure whether the destination Workbook is already open or not. The following excel vba function assigned to check whether a workbook we need is open...

  • How To Hide an Excel Worksheet

    Some time we want to hide a certain Excel Worksheet from view, and it’s a common practice to use select the Worksheet, go to menu Format > Sheet > Hide. Using the method explained above is right, but unfortunately, others people can easily unhide the Worksheet using the same method, only this time, instead of selecting...

Valuable Resources

If you found this page useful, please consider bookmark it using social media or add a link to this page.

  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • connotea
  • Diigo
  • email
  • Fark
  • Identi.ca
  • Live
  • MisterWong
  • MySpace
  • Netvibes
  • NewsVine
  • PDF
  • Ping.fm
  • Propeller
  • Reddit
  • Simpy
  • StumbleUpon
  • Technorati
  • Twitter
  • Wikio
  • Yahoo! Bookmarks