Consulting

Results 1 to 4 of 4

Thread: Returning focus to previous workbook after opening another workbook

  1. #1
    VBAX Newbie
    Joined
    Nov 2008
    Location
    Prince George, BC
    Posts
    2
    Location

    Question Returning focus to previous workbook after opening another workbook

    Thank you for reading this, my first post here.

    This is the code (at bottom of the message) I have, with the "IsXLBookOpen" code borrowed from an article I read here yesterday. The following is code I have in a workbook (lets call it "MASTER") that needs the workbook mentioned ( lets call it "P&S") in it opened to look up data in it. So what happens now, I open MASTER and the file P&S is automatically opened, but the "focus" stays with the newly opened P&S. I realize I could use
    [vba]
    Windows("MASTER.xls").Activate
    [/vba]
    but I copy and rename this file quite often so I do not want to have to edit the code each time. Is there a way to make this "generic"? meaing, that when I open up MASTER (after it has say been copied and renamed for the umpteenth time to say MASTER333) that it will automatically open the P&S workbook and then have focus go back to the workbook that called for the file to be opened.

    [vba]
    Private Sub Workbook_Open()

    If IsXLBookOpen("-=Products&Suppliers 2008=-.xls") = False Then Workbooks.Open "C:\NORCAP\-=Products&Suppliers 2008=-.xls"

    End Sub

    [/vba]

  2. #2
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    When you open the new workbook using code use this refering to the newly opened workbook

    [vba]Windows("2.xls").ActivatePrevious[/vba]
    Hope this helps
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  3. #3
    VBAX Newbie
    Joined
    Nov 2008
    Location
    Prince George, BC
    Posts
    2
    Location
    When I placed this code in the workbook doing hte calling, I got a subscript out of range error. When I placed it in the workbook being opened:
    [VBA]
    Private Sub Workbook_Open()
    Windows("-=Products&Suppliers 2008=-.xls").ActivatePrevious
    End Sub

    [/VBA]
    it worked. Just want to make sure that I have it in the correct spot. Thanks for the quick reply.

  4. #4
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    When i tried it i had a workbook on my desktop called "1" with the code in, i also had a workbook called "2". I had the code
    [VBA]Windows("2.xls").ActivatePrevious [/VBA]
    in a module in workbook "1" just after the part where i opened workbook "2". If that makes sence
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

Posting Permissions

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