+ Reply to Thread
Results 1 to 5 of 5

Workbook.Activate not firing when workbook opens

Hybrid View

  1. #1
    Registered User
    Join Date
    09-04-2018
    Location
    Birmingham
    MS-Off Ver
    2010
    Posts
    3

    Workbook.Activate not firing when workbook opens

    Hi.

    I have converted an old Excel 2003 spreadsheet with VBA in it to an Excel 2010 macro enabled workbook.

    The spreadsheet has some code to create a custom menu when the workbook is activated (or remove it when deactivated) but the code is not firing when I first open the workbook. I can create a new one and then switch back which then runs the code. I have even added a Workbook Open event to run the code but this doesnt work either.

    Can anyone help me with why the code isn't running?

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,995

    Re: Workbook.Activate not firing when workbook opens

    I suspect that the event is firing but the menu code won't work in versions later than 2003. Can you show the actual code? (Please use code tags to paste code into a post)

    One way to test this is to put this as your first line in the Sub and see if the message box comes up:
    MsgBox "Event starting"
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    09-04-2018
    Location
    Birmingham
    MS-Off Ver
    2010
    Posts
    3

    Re: Workbook.Activate not firing when workbook opens

    Hi.

    Thanks for the response. I have already tried that and the message box doesn't appear. If I step through the code the menu gets created so I don't think it's a compatibilty issue.

    I can call the function from the Worksheet_SelectionChange event which also creates it. It's the code not running when it's first opened I am struggling with.

    Private Sub Workbook_Activate()
        ' Add the extra menu.
        ' Note that Workbook_Deactivate removes the menu so that it is only available while the user
        '  is working on this workbook.
        
        On Error GoTo PROC_ERR
        
        Dim cbrMainMenu As CommandBar
        Dim cbpConQuest As CommandBarPopup
        Dim cbpWindow As CommandBarPopup
        Dim cbpExport As CommandBarPopup
        Dim cmdValidate As CommandBarButton
        Dim cmdExportIX As CommandBarButton
        Dim cmdExportEmail As CommandBarButton
        
        Set cbrMainMenu = Application.CommandBars.Item("Worksheet Menu Bar")
        
        ' Remove the  menu from the main menu if one already exists.
        On Error Resume Next
        Call cbrMainMenu.Controls.Item("&EFT Export").Delete
        On Error GoTo PROC_ERR
        
        Set cbpWindow = cbrMainMenu.Controls.Item("&Window")
        
        ' Create the new menu.
        Set cbpConQuest = cbrMainMenu.Controls.Add( _
            Type:=msoControlPopup, _
            Before:=cbpWindow.Index _
        )
        Let cbpConQuest.Caption = "&EFT Export"  
        ' Export for sending via email.
        Set cmdExportEmail = cbpConQuest.Controls.Add( _
            msoControlButton _
        )
        Let cmdExportEmail.Caption = "Export to QMP"
        Let cmdExportEmail.OnAction = "ExportAsCSV_Email"
        
    PROC_EXIT:
        Exit Sub
        
    PROC_ERR:
        Call MsgError( _
            strModule:=mc_strModuleName, _
            strProcedure:="Workbook_Activate", _
            lngErrNumber:=Err.Number, _
            strErrSource:=Err.Source, _
            strErrDescription:=Err.Description _
        )
        GoTo PROC_EXIT
    End Sub
    Last edited by fbxiii; 09-05-2018 at 10:06 AM.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,995

    Re: Workbook.Activate not firing when workbook opens

    Which module is the code in?

  5. #5
    Registered User
    Join Date
    09-04-2018
    Location
    Birmingham
    MS-Off Ver
    2010
    Posts
    3

    Re: Workbook.Activate not firing when workbook opens

    ThisWorkbook in Workbook_Activate

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Loop through workbooks and print one worksheet - workbook opens but not as active workbook
    By nigelog in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-11-2017, 06:26 AM
  2. Have workbook open specific worksheet when workbook opens
    By snuffnchess in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-15-2016, 07:39 PM
  3. Replies: 1
    Last Post: 01-13-2016, 06:06 PM
  4. Opens another workbook but doesn't activate it, ignores ScreenUpdating = False
    By natefarm in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-01-2015, 11:24 AM
  5. [SOLVED] Need a macro that opens another workbook but stays in current workbook
    By shoes1 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-28-2015, 08:44 PM
  6. how can i execute a macro in current workbook after another workbook opens?
    By sds1126 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-12-2014, 09:36 AM
  7. Replies: 1
    Last Post: 03-10-2014, 05:20 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1