+ Reply to Thread
Results 1 to 5 of 5

VBA - Run code on workbook first open

  1. #1
    Registered User
    Join Date
    08-31-2011
    Location
    Bristol
    MS-Off Ver
    Excel 2007
    Posts
    43

    VBA - Run code on workbook first open

    I'd like some code to run the first time someone opens the file. It's an .xltm file (could be .xlsm though).

    How do I execute code the first time someone opens the book, but not the second onwards? I have googled and found people using
    Please Login or Register  to view this content.
    But I don't understand the code. Is it relevant to my problem? Could someone explain/translate it to English?

    I want the code to run a form (simple enough) and also set the file name but NOT actually open the dialogue to save the file.
    So when they open the workbook, a form comes up, they enter some details eg. Date and their name and press OK.

    Later when they go to save the file, it should do a save as, and the file name should be <name> - <date>.xlsm

  2. #2
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: VBA - Run code on workbook first open

    Hi,

    Go to ThisWorkbook and double-click... on the top you will find two drop-down menus. Set the 1st to read "Workbook" and the second to "Open". This code will appear:

    Please Login or Register  to view this content.
    The code to update the file name will go in here:

    Please Login or Register  to view this content.

    abousetta
    Last edited by abousetta; 03-07-2012 at 11:40 AM.
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  3. #3
    Registered User
    Join Date
    08-31-2011
    Location
    Bristol
    MS-Off Ver
    Excel 2007
    Posts
    43

    Re: VBA - Run code on workbook first open

    The code for Sub Workbook_Open() runs every time I open the file. Is there a way that I can get it to only open the first time someone opens the work book?

    The file is a template, so a save as will happen if the user wants to save it.

  4. #4
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: VBA - Run code on workbook first open

    Use a hidden sheet that the workbook will check each time it opens. For example in Range(A1) of the hidden sheet:

    The first time the code runs it will change the yes to a no.

    Please Login or Register  to view this content.

  5. #5
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: VBA - Run code on workbook first open

    Hi @Gaz_m2k5,

    Glad it all worked out. If you are satisfied with the responses then please mark the thread as Solved.

    Good luck.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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