+ Reply to Thread
Results 1 to 19 of 19

Close Workbook with Application.OnTime

  1. #1
    Registered User
    Join Date
    06-26-2006
    Location
    New York
    MS-Off Ver
    Office Pro 2003 primarily, some 2007
    Posts
    85

    Close Workbook with Application.OnTime

    I didn't receive a response to a previous post where I was trying to use a batch file to close and open a workbook at a scheduled time everyday. I was thinking about this problem over the weekend, and thought that it would probably be easier to just use a macro to close the file at a certain time everyday (if it is open) and then use a batch file to open the file a couple of minutes later.

    I'm having problems with what I thought should be pretty simple though. I tried this to close the file at 5:15pm

    Please Login or Register  to view this content.
    I sat and watched as 5:15 rolled around, but nothing happened. (The title bar in the VBA code window is constantly flashing {Module 2 (Code)] though)

    Any suggestions on why this isn't working?
    Last edited by dylanemcgregor; 07-30-2009 at 11:52 AM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    re: Close Workbook with Application.OnTime

    A place to start is with Help for the OnTime method, Dylan. Your code doesn't compile, and Help has good examples for how to both schedule and unschedule tasks.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    06-26-2006
    Location
    New York
    MS-Off Ver
    Office Pro 2003 primarily, some 2007
    Posts
    85

    Re: Close Workbook with Application.OnTime

    Hi shg,

    Thanks as always for the help. I have read through the ontime method section of help, and it seems that I was missing the call to procedure argument? As everything else is optional?

    I have modified my code to this, and it compiles, but still does not run at the scheduled time.

    Please Login or Register  to view this content.
    I had originally started with and tried to modify the below code, which I found on another site, but had tried to leave off the "OpenMe" argument since I just wanted to close the file.

    Please Login or Register  to view this content.

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Close Workbook with Application.OnTime

    Here's a hint: by itself in Excel, 9:15PM is probably about the time people were headed for bed. TV hadn't been invented, so there wasn't much else to do.

    Take a look at Help again.

  5. #5
    Registered User
    Join Date
    07-03-2007
    Location
    Canberra, Australia
    MS-Off Ver
    2010
    Posts
    60

    Re: Close Workbook with Application.OnTime

    Hi dylanemcgregor,

    Try this web page, this will only close the workbook the code does not close the Excel instance, there was a thread for this at

    http://www.excelforum.com/excel-prog...and-excel.html

    http://www.ozgrid.com/Excel/run-macro-on-time.htm
    Kerno

  6. #6
    Registered User
    Join Date
    06-26-2006
    Location
    New York
    MS-Off Ver
    Office Pro 2003 primarily, some 2007
    Posts
    85

    Re: Close Workbook with Application.OnTime

    Thanks to both of you, and sorry for the delayed reply, I just now got back in front of the Excel workbook and was able to try a few more things.

    shg, I appreciate that you are trying to lead me to the solution, without telling it to me outright, as I do want to learn to be a little more self sufficient with VBA...but I'm afraid I'm still not getting it. Based on your hint I tried to play around with the formatting of the time value, first removing the space before the PM. I tried 12:05PM as well as 12:05:00PM. My latest version has dropped the PM entirely, and instead is using a 24hr clock as per the examples in help.

    Please Login or Register  to view this content.
    I'm still not having any luck. I assume I still have my format wrong and have misinterpreted your hint...but all the examples I'm finding on-line seem to use the same format, so I'm definitely confused.

    If on the off chance that my format is right, could it be another process that is interfering with letting ontime run? I've read that it will not run if another macro is running, but what if the sheet is just constantly recalculating? (This is a real time feed sheet that is constantly updating info from Bloomberg)

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Close Workbook with Application.OnTime

    In Excel date/time, the whole number is days since 0 Jan 1900, and the decimal portion is time. You're scheduling the macro to run on New Year's Eve 1900. Try scheduling for Date + Time instead of just Time.

    In addition, if Excel remains open, but the workbook was closed earlier (but after the close task was scheduled), the workbook will reopen, just so it can close -- like the nurse waking you up in the hospital to give you a sleeping pill.

    So you need to put the schedule time in a public variable, and in the BeforeClose event, unschedule the close task if it is currently scheduled.
    Last edited by shg; 07-29-2009 at 12:59 PM.

  8. #8
    Registered User
    Join Date
    06-26-2006
    Location
    New York
    MS-Off Ver
    Office Pro 2003 primarily, some 2007
    Posts
    85

    Re: Close Workbook with Application.OnTime

    Quote Originally Posted by shg View Post
    In Excel date/time, the whole number is days since 0 Jan 1900, and the decimal portion is time. You're scheduling the macro to run on New Year's Eve 1900. Try scheduling for Date + Time instead of just Time.
    From the VBA help under OnTime Method I see

    This example runs my_Procedure at 5 P.M.

    Please Login or Register  to view this content.
    My understanding was this should run "my procedure" everyday at 5pm? Would the example just run "my procedure" once, on 1/1/1900 at 5pm? And since that time has obviously passed, it wouldn't run at all?

    In addition, if Excel remains open, but the workbook was closed earlier (but after the close task was scheduled), the workbook will reopen, just so it can close -- like the nurse waking you up in the hospital to give you a sleeping pill.
    I don't mind if this happens, and in fact it is probably preferable that it works that way, as I would like to make sure that it is open before I close it.

  9. #9
    Registered User
    Join Date
    06-26-2006
    Location
    New York
    MS-Off Ver
    Office Pro 2003 primarily, some 2007
    Posts
    85

    Re: Close Workbook with Application.OnTime

    Rereading the Ozgrid page that Kerno linked to seems to explicitly say that I should be able to use the below code to run "my macro everyday.

    Please Login or Register  to view this content.
    This will run the Procedure MyMacro at 15:00 each day
    But I have also come across another example, which seems to be closer to what shg is suggesting.

    Please Login or Register  to view this content.
    The macro in that example is never completely placed all together, so I am having trouble figuring it all out, but he seems to be using public variables for the date as well, and then using two ontime statements, as well as referencing cells for the actual time?
    Last edited by dylanemcgregor; 07-29-2009 at 02:36 PM.

  10. #10
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Close Workbook with Application.OnTime

    Quote Originally Posted by dylanemcgregor View Post
    Please Login or Register  to view this content.
    Your code should work fine - if you don't specify the day, today will be assumed.
    How are you running the CloseMe sub? Manually, via a menu, shortcut key or what?
    Remember what the dormouse said
    Feed your head

  11. #11
    Registered User
    Join Date
    06-26-2006
    Location
    New York
    MS-Off Ver
    Office Pro 2003 primarily, some 2007
    Posts
    85

    Re: Close Workbook with Application.OnTime

    Quote Originally Posted by romperstomper View Post
    Your code should work fine - if you don't specify the day, today will be assumed.
    How are you running the CloseMe sub? Manually, via a menu, shortcut key or what?
    Via an Open Workbook event. I had finally gotten it to run partially in a test document, but was having a problem reopening the file again after the macro ran (it would just auto close as soon as it opened). I had added in a
    Please Login or Register  to view this content.
    argument, and getting rid of the Date + part and going back to how I had it originally seems to have worked. I'm definitely going to have to run with this a few days to gain some confidence, but for anyone else who is looking to do this. Here is what I ended up with:

    In ThisWorkbook
    Please Login or Register  to view this content.
    In a Module
    Please Login or Register  to view this content.

  12. #12
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Close Workbook with Application.OnTime

    Incidentally, if you want the workbook reopened, you could simply schedule another macro before closing. Excel will automatically reopen the workbook at the relevant time, as long as you haven't shut Excel down in the meantime.

  13. #13
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Close Workbook with Application.OnTime

    ... if you don't specify the day, today will be assumed.
    I did not know that, thank you.

  14. #14
    Registered User
    Join Date
    06-26-2006
    Location
    New York
    MS-Off Ver
    Office Pro 2003 primarily, some 2007
    Posts
    85

    Re: Close Workbook with Application.OnTime

    Thanks for the suggestion. I'd originally tried a simple reboot macro that I'd found.

    Please Login or Register  to view this content.
    The workbook that this is for has a number of cells that update realtime as the sheet is open, but there is one table that I've only been able to update by closing and reopening the sheet. I thought I could use the above code to quickly restart the workbook, but when the sheet reopened the links don't update (which is weird because normally they update even if I tell them to not update).

    Anyway, I think I'll get better results using a combination of a scheduled task and a batch file to launch the workbook a few minutes after I close it.

  15. #15
    Registered User
    Join Date
    06-26-2006
    Location
    New York
    MS-Off Ver
    Office Pro 2003 primarily, some 2007
    Posts
    85

    Re: Close Workbook with Application.OnTime

    Argh...One more question.

    The whole point of doing this is so that I can run a macro when I open the file with the batch file...but can I have multiple workbook open events in one workbook? One that will run whenever the workbook is open, but the second one that I can set to just run if the workbook is open at a given time? Or do I just use one Workbook Open and put both calls in one event? Something like this?

    Please Login or Register  to view this content.

  16. #16
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Close Workbook with Application.OnTime

    You can only have one Workbook_Open event.

    Thinking about it, I suspect it is more accurate to sa that with OnTime, if you don't specify a day part, it will use the next occurrence of the specified time. So if you schedule prior to that time, it will assume today, otherwise it will assume tomorrow.
    I'll be double-checking that in about 5 minutes or so...

    Addendum: yes, seems to be the case. Scheduled for 00:00:01 and it ran the following morning.
    Last edited by romperstomper; 07-29-2009 at 07:04 PM.

  17. #17
    Registered User
    Join Date
    06-26-2006
    Location
    New York
    MS-Off Ver
    Office Pro 2003 primarily, some 2007
    Posts
    85

    Re: Close Workbook with Application.OnTime

    Quote Originally Posted by romperstomper View Post
    You can only have one Workbook_Open event.
    So would something like I have in the above post not work? I've been trying to test, but it involves a lot of moving parts, and getting everything changed at one time has been proving difficult (I accidentally broke my workbook and lost all macros and had to restore from a backup)

    If I can't put two arguments in a workbook_open event, any other suggestions on how to get one macro to run at one time during the day, and another one to run at another time without any human interaction?

  18. #18
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Close Workbook with Application.OnTime

    You can use all the normal syntax in a Workbook_Open event, I was just saying you can only have one Workbook_Open procedure. That sort of If test shoud be fine for determining which macro to run.

  19. #19
    Registered User
    Join Date
    06-26-2006
    Location
    New York
    MS-Off Ver
    Office Pro 2003 primarily, some 2007
    Posts
    85

    Re: Close Workbook with Application.OnTime

    Thank you all very much. I just had my first successful test. Just need to change the times now (hope that doesn't screw anything up) and then set about figuring out how to add in even more useful features...but think I'll wait a couple of days for that.

    Marking this thread as closed.

+ 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