none
Run macro workshet activate macro on workbook open RRS feed

  • Question

  • Hi,

    I am using NPOI to generate an Excel workbook from an template.  The template contains a VBA project.  The  Workbbok code has the following Workbook_Open event code:

    Private Sub Workbook_Open()
       Worksheets("MyOpps(2)").Activate
    End Sub
     

    The MyOpps(2) worksheet contains code that is run in the Worksheet_Activate method

    tMyOpps(2) is the only worksheet in the workbook. When I generate the workbook, save it and open it the Workbook_Open macro is not being run. 

    I have signed the VBA project in the workbook and set the macro security to "Disable all macros except digitally signed macros".

    If I create a new worksheet in the workbook then click back to the MyOpps(2)  worksheet the macro runs.  Is there anyway to get the macro to run when the workbook is opened?

    Monday, July 25, 2016 8:38 PM

Answers

  • Hi mohunt,

    you had mentioned that when you open the workbook you want to run the code placed in Sheet "MyOpps(2)".

    so Follow the steps below.

    make a new sub in Sheet "MyOpps(2)". then cut the code from Worksheet_Activate() event and place it inside the newly created sub.

    then go to the Workbook_Open() event. and call the sub there like below.

    Private Sub Workbook_Open()
    'Worksheets("MyOpps(2)").Activate
    Call Sheet1.demo
    End Sub
    

    now when you open the workbook it will every time calls that code.

    Regards

    Deepak


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by mohunt Tuesday, July 26, 2016 11:55 AM
    Tuesday, July 26, 2016 2:58 AM
    Moderator

All replies

  • Hi mohunt,

    you had mentioned that when you open the workbook you want to run the code placed in Sheet "MyOpps(2)".

    so Follow the steps below.

    make a new sub in Sheet "MyOpps(2)". then cut the code from Worksheet_Activate() event and place it inside the newly created sub.

    then go to the Workbook_Open() event. and call the sub there like below.

    Private Sub Workbook_Open()
    'Worksheets("MyOpps(2)").Activate
    Call Sheet1.demo
    End Sub
    

    now when you open the workbook it will every time calls that code.

    Regards

    Deepak


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by mohunt Tuesday, July 26, 2016 11:55 AM
    Tuesday, July 26, 2016 2:58 AM
    Moderator
  • Thank you, your suggestion  worked.
    Tuesday, July 26, 2016 11:55 AM