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.

This entry was posted in Workbook and Worksheet and tagged , , , . Bookmark the permalink.

18 Responses to Automatically Run an Excel Macro

  1. Seli says:

    I have some code that works within one Sub, I would like to call another macro but when I tried to use call VBA wanted to set up another sub which sits outside the sub I wanted to run it in. Is there a way I can call a macro into another sub and have it run?

    Geoff

  2. Poer says:

    Hi Seli, ofcourse we can call another macro, just use Call or directly use the function/sub name.

    Also please make sure that you use public sub/function if you want it can be called from another module.

  3. Krish says:

    Hi

    I tried. but its not working.. i want to run the macro every time (ex: while i navigate sheet1 to sheet2).

    please help.

    Thanks in Advance
    Krish

  4. Poer says:

    Halo Krish, if you need to run the macro each time an excel worksheet activated, you need to put the calling code in Sheet1 module, in event Activate like this:

    Private Sub Worksheet_Activate()
       call onSheet1Activate
    End Sub
    
  5. adi says:

    Hi, I don’t understand where I’m suppose to add
    the code:
    Private Sub Workbook_Open ()

    Call onFileOpenMacro

    End Sub

    In the same sheet of the macro I want to activate
    but in the top???
    thanks:)

  6. adi &rauquo; double click your VBAProject > ThisWorkbook module, as put it there or click object combo box in the right pane and select Workbook.

  7. ajit says:

    Its not working?

    Public Sub Workbook_Open()

    Call onFileOpenMacro

    End Sub

    Public Sub onFileOpenMacro()
    MsgBox (“yo”)
    End Sub

  8. Ajit says:

    hi,
    i want’s to create an excel file where if i key in a serial number in the cell, the file will automaticaal come and attach to the cell. I dont want to go for Hyperlink.
    Any suggestions or help pls.
    Ajit

  9. Suresh Kumar says:

    hi dears,

    I need to run a macro when ever i open excel file.

    Tha above procedure i have tried it is not working.
    Could you please tell me step by step

  10. Jordan Jubilo says:

    Its not working?

    Public Sub Workbook_Open()

    Call onFileOpenMacro

    End Sub

  11. Pingback: reverse mortgages

  12. Pingback: information about reverse mortgages

  13. Pingback: cruises from new york

  14. Pingback: acid reflux symptoms

  15. Pingback: Bill Maher Tour Dates

  16. Pingback: Bill Maher Tour Dates

  17. Pingback: kohler tubs

  18. Pingback: courts furniture

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>