+ Reply to Thread
Results 1 to 18 of 18

Opening and activating a file in VBA

  1. #1
    Registered User
    Join Date
    04-16-2014
    Location
    Woodstock, New Brunswick
    MS-Off Ver
    Excel 2003
    Posts
    10

    Opening and activating a file in VBA

    Hi,
    I have an excel file that I am making that is a master file that calls and opens other files. My only problem is that when excel opens the new file it reactivates the master file instead of activating the file that was just opened. I want to be able to open the file, have the file become activated and then when I close the file I want the master file to be activated or open? Here is my present code, can anyone help me with this?

    Please Login or Register  to view this content.
    Last edited by alansidman; 04-21-2014 at 09:14 AM. Reason: code tags

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,945

    Re: Opening and activating a file in VBA

    Are you running this from Excel or another application? You should be just using this to open the file in the current Excel instance
    Please Login or Register  to view this content.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Registered User
    Join Date
    04-16-2014
    Location
    Woodstock, New Brunswick
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Opening and activating a file in VBA

    I want to open the new file (Fedex) in a new instance of excel. Then when I close the "fedex" file, i want the "dispatch" userform to show. Is there any way to do this?

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,879

    Re: Opening and activating a file in VBA

    Justin

    Code Tags Added
    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between
    Please Login or Register  to view this content.
    tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found at http://www.excelforum.com/forum-rule...rum-rules.html



    (Because you are new to the forum, I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  5. #5
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,945

    Re: Opening and activating a file in VBA

    Using two instances of Excel in code is unnecessary and make the code much more complicated, to the point that I don't want to continue in this thread. Is there a valid reason why you want Fedex opened in its own instance of Excel?

  6. #6
    Registered User
    Join Date
    04-16-2014
    Location
    Woodstock, New Brunswick
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Opening and activating a file in VBA

    The main reason is so the the macro won't make changes on the wrong workbook.

  7. #7
    Registered User
    Join Date
    04-16-2014
    Location
    Woodstock, New Brunswick
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Opening and activating a file in VBA

    Thanks for the changes, sorry about that.

  8. #8
    Registered User
    Join Date
    04-16-2014
    Location
    Woodstock, New Brunswick
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Opening and activating a file in VBA

    Your code works great, but I only want it to show the "Dispatch" userform after the "FedEx" workbook is closed. Can you help me with how to do that?

  9. #9
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,945

    Re: Opening and activating a file in VBA

    That is no reason if you properly create and use objects in your code, both in the source workbbook and in Fedex.xlsm. Typically, you create an Excel workbook object like this: In a standard codemodule:

    Please Login or Register  to view this content.
    Then in your un-named userform's codemodule
    Please Login or Register  to view this content.
    Then you can use the workbook object to direct your code to work on that workbook:

    Please Login or Register  to view this content.
    or other workbook objects that are available, like the workbook with the code:
    Please Login or Register  to view this content.
    So then the form could be shown using the activate event of the original workbook - in the codemodule of the ThisWorkbook object

    Please Login or Register  to view this content.
    Last edited by Bernie Deitrick; 04-21-2014 at 10:47 AM.

  10. #10
    Registered User
    Join Date
    04-16-2014
    Location
    Woodstock, New Brunswick
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Opening and activating a file in VBA

    Thanks so much for writing up all that code. I copied the code into my userform, but it errors on:
    Please Login or Register  to view this content.
    I also tried putting this code in a module and it didn't seem to work either, can you give me any direction on this?

  11. #11
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,945

    Re: Opening and activating a file in VBA

    Did you insert a standard codemodule? Just use the Insert menu in the VBE...

  12. #12
    Registered User
    Join Date
    04-16-2014
    Location
    Woodstock, New Brunswick
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Opening and activating a file in VBA

    I inserted it into a new module like this:
    Please Login or Register  to view this content.
    But now it errors on this line of code:
    Please Login or Register  to view this content.

  13. #13
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,945

    Re: Opening and activating a file in VBA

    Put just this at the top of a standard codemodule:

    Please Login or Register  to view this content.
    That is, remove the "Sub boolShowForm()" and the "End Sub"

    Your declarations must be public to the project as intended, so they just go it alone at the top. Read more - and learn more - about variable scope in VBA at this page:

    http://support.microsoft.com/kb/141693/en-us

  14. #14
    Registered User
    Join Date
    04-16-2014
    Location
    Woodstock, New Brunswick
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Opening and activating a file in VBA

    The code works now, but the "Fedex" file does not show. The "dispatch" userform shows and the "fedex" file can only be accessed once the "dispatch" userform is closed. Anyway to make the "fedex" file pop up automatically?

  15. #15
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,945

    Re: Opening and activating a file in VBA

    The dispatch userform should not show until the workbook's window is activated when Fedex is closed. Where did you put the event code?

  16. #16
    Registered User
    Join Date
    04-16-2014
    Location
    Woodstock, New Brunswick
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Opening and activating a file in VBA

    I put all the code into the userform button that calls "fedex".
    Please Login or Register  to view this content.
    This is probably where I am going wrong. Where should I put the code?

  17. #17
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,945

    Re: Opening and activating a file in VBA

    I gave clear directions in my post.

    Change your sub to just this, as I originally showed:
    Please Login or Register  to view this content.
    Then put this code into the codemodule of the ThisWorkbook object: but first, read more about events and how to code them at

    http://dmcritchie.mvps.org/excel/event.htm

    and a much more advanced discussion at

    http://www.cpearson.com/excel/Events.aspx

    Please Login or Register  to view this content.
    Last edited by Bernie Deitrick; 04-21-2014 at 02:24 PM.

  18. #18
    Registered User
    Join Date
    04-16-2014
    Location
    Woodstock, New Brunswick
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Opening and activating a file in VBA

    Ok, thanks so much for your help! I should be able to take it from here. Thanks again!

+ 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. Help in Opening a Workbook and Activating it
    By hungyzerglin in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-10-2012, 04:04 PM
  2. Opening file message when opening a file from an http address
    By The_Real_MerlinXL in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-26-2010, 12:33 PM
  3. Activating Sheets in another file and writing to them
    By weaseljw17 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-20-2006, 09:04 PM
  4. Activating "Todays Date" column upon opening?
    By Jeremy H via OfficeKB.com in forum Excel General
    Replies: 3
    Last Post: 08-24-2005, 10:05 PM
  5. [SOLVED] Activating cell in a file
    By Roger in forum Excel General
    Replies: 2
    Last Post: 07-07-2005, 07:05 PM

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