+ Reply to Thread
Results 1 to 30 of 30

Macro that saves workbook every 4 minutes

  1. #1
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    846

    Macro that saves workbook every 4 minutes

    Evening happy campers,

    I have the following code:
    Please Login or Register  to view this content.
    Which saves my workbook every 4 minutes, however - i have 2 problems.

    1) it also saves any other workbook that is open
    2) if i have more than one workbook open, and close the one with this particular code, the code re-opens the sheet and saves it again and again, until i fully close excel. (which is annoying as i often have lots of sheets open)

    Things to mention - The workbook's name changes daily, as we 'Save As' a new name that includes the date. (why name always changes)
    How can i avoid this - is it even possible?

    Thanks, galvinpaddy

  2. #2
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Macro that saves workbook every 4 minutes

    You could try using another sub that will set the workbook, and instead of you running SaveThis sub once, you run that other one which will be the starting kick. For example:

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Hope it helps.
    .?*??)
    `?.???.?*??)?.?*?)
    (?.?? (?.?
    Pichingualas <---
    ??????????????????????????

    Wrap your code with CODE TAGS.
    Thank those who helped you, Don't forget to add to their REPUTATION!!! (click on the star below their post).
    Please mark your threads as [SOLVED] when they are (Thread Tools->Mark thread as Solved).

  3. #3
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Macro that saves workbook every 4 minutes

    This should solve all your issues:

    Please Login or Register  to view this content.
    This code MUST go inside ThisWorkbook corresponding code sheet in the VBA interface.

    I hope that helps.

  4. #4
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Macro that saves workbook every 4 minutes

    Remember that now you won't be running SaveThis, but GetSavesStarted instead (which will in turn run SaveThis).

  5. #5
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Macro that saves workbook every 4 minutes

    Quote Originally Posted by galvinpaddy View Post
    1) it also saves any other workbook that is open
    ...
    The workbook's name changes daily, as we 'Save As' a new name that includes the date.
    To avoid this, check whether ThisWorkbook.Name = the expected workbook name that includes the date. What is the file name of the new workbook that includes the date (give an example)? In the code below I've assumed the workbook file name is in the format "Workbook yyyy-mm-dd.xls", e.g. "Workbook 2012-05-03.xls" for today.

    Quote Originally Posted by galvinpaddy View Post
    2) if i have more than one workbook open, and close the one with this particular code, the code re-opens the sheet and saves it again and again, until i fully close excel.
    To avoid this, cancel the OnTime procedure in the Workbook_BeforeClose event handler.

    I've incorporated both these fixes in the code below:

    Put the following code in the ThisWorkbook module
    Please Login or Register  to view this content.
    Put the following code in a standard module e.g. Module1
    Please Login or Register  to view this content.
    Post responsibly. Search for excelforum.com

  6. #6
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Macro that saves workbook every 4 minutes

    ThisWorkbook.Name does not include ".xls" in it...

  7. #7
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Macro that saves workbook every 4 minutes

    Quote Originally Posted by Pichingualas View Post
    ThisWorkbook.Name does not include ".xls" in it...
    It does for me (Excel 2003), as demonstrated by this in the Immediate window:

    ?thisworkbook.Name
    OnTime Save Workbook.xls

    Anyway, if you are correct for the OP's Excel version I'm sure there's an easy way to modify my code to handle this.
    Last edited by Chippy; 05-03-2012 at 07:08 PM.

  8. #8
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    846

    Re: Macro that saves workbook every 4 minutes

    Wow thanks all for the responses.
    My version is 2010, the work book name will be 4th May 2012 Production Schedule, with the only changes being the day and month.
    Would it be easier to change the format of the name?
    @Pichingualas - i have input your code into 'ThisWorkbook' but it does not appear to run. the code i have entered is
    Please Login or Register  to view this content.
    Changed time to 30 seconds just to test but no save attempt made within 5 minutes.

  9. #9
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    846

    Re: Macro that saves workbook every 4 minutes

    Apologies, it does run but i must first run the macro GetSavesStarted.
    This however only runs once then brings up an error stating that the macro cannot be run as it may not be available in workbook or macros are disabled??

    My original code worked how i wanted just with some additionally unwanted extras!! i want the code to activate when the workbook is opened, save every minute (or however long its set) then stop running once sheet is closed.

  10. #10
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    846

    Re: Macro that saves workbook every 4 minutes

    @ CHippy -

    Have tried the following for your suggestion
    Module1 -
    Please Login or Register  to view this content.
    ThisWorkbook -
    Please Login or Register  to view this content.

  11. #11
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    846

    Re: Macro that saves workbook every 4 minutes

    @Chippy - with yours fella, it flat out wont run. have the changed i made affected it code too much?

  12. #12
    Forum Contributor
    Join Date
    01-09-2009
    Location
    Cedar Hill, Tx
    MS-Off Ver
    Excel 2003
    Posts
    200

    Re: Macro that saves workbook every 4 minutes

    I take it that the AutoRecover won't meet your needs?

  13. #13
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    846

    Re: Macro that saves workbook every 4 minutes

    Unfortunately no, the sheet is saved on a shared drive at work and for some reason the auto save/recover does not function.
    Hence the diving into VBA

  14. #14
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Macro that saves workbook every 4 minutes

    Quote Originally Posted by galvinpaddy View Post
    the work book name will be 4th May 2012 Production Schedule, with the only changes being the day and month.
    But your code says:
    Please Login or Register  to view this content.
    which gives "Production Schedule 04-05-2012", therefore the code inside the If statement won't run if the workbook is named as you first said.

    Try debugging with F8 to see whether the If statement is working as expected, or put MsgBox ">" & ThisWorkbook.Name & "<" before it. Or try the following If statement instead:
    Please Login or Register  to view this content.
    Make sure the workbook name in this If statement reflects exactly the actual workbook name, as I'm not sure what the name really is, given the 2 variants you've posted.

    I don't have Excel 2010, but the code works perfectly in Excel 2003.

  15. #15
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    846

    Re: Macro that saves workbook every 4 minutes

    Sorry, can see i seriously confused it there. try again lol
    Sheet name - 20-May-2012 Production Schedule
    ThisWorkbook
    Please Login or Register  to view this content.
    Module 1
    Please Login or Register  to view this content.
    Hopefully that shows all the same info

  16. #16
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    846

    Re: Macro that saves workbook every 4 minutes

    The code does not appear to run automatically. have i missed something?

  17. #17
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Macro that saves workbook every 4 minutes

    Quote Originally Posted by galvinpaddy View Post
    Sheet name - 20-May-2012 Production Schedule
    But that date is some days in the future. Also, 'sheet name'? The code is using workbook names.
    Please Login or Register  to view this content.
    And the line above is using the current date, which gives 04-May-2012 Production Schedule and therefore the names are still inconsistent. I suggest again using F8 and/or the MsgBox line to debug.

    The code does not appear to run automatically. have i missed something?
    The code runs automatically from the Workbook_Open handler.

  18. #18
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    846

    Re: Macro that saves workbook every 4 minutes

    Sry fella, i truly am an idiot. ok, date sorted.
    Sheetname - was actually supposed to read Workbook name. (04-may-2012 Production Schedule)

    Whilst pressing F8 i received an error msg - 'Cannot execute in break mode' which i can only assume meant it was working.

    Something that confuses me though, i thought that when you saved something you could no longer click undo? as even when i manually save it, i can still undo data entry - how come?
    Also, i have adjusted the timer to 15 seconds, but there just doesnt seem to be a save action happening?

    Apologies for the dumbness and thanks for your patience fella!
    Last edited by galvinpaddy; 05-04-2012 at 02:33 PM.

  19. #19
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Macro that saves workbook every 4 minutes

    Quote Originally Posted by galvinpaddy View Post
    Workbook name. (04-may-2012 Production Schedule)
    That name is still inconsistent with the code, but I'm sure you can spot the difference (look carefully).
    Whilst pressing F8 i received an error msg - 'Cannot execute in break mode' which i can only assume meant it was working.
    Put a breakpoint (F9) on the first line of SaveThis, then F5 (run) and then press F8 when the debugger breaks in the code.

    For testing, you can run Workbook_Open from the debugger, you don't need to keep closing and reopening the workbook to make it run. But make sure the timer is not running - run StopTimer first.

  20. #20
    Forum Contributor
    Join Date
    01-09-2009
    Location
    Cedar Hill, Tx
    MS-Off Ver
    Excel 2003
    Posts
    200

    Re: Macro that saves workbook every 4 minutes

    Our corporate setup is the same and AutoRecover works fine. You might check that it's setup correctly. Otherwise, good luck.

    Quote Originally Posted by galvinpaddy View Post
    Unfortunately no, the sheet is saved on a shared drive at work and for some reason the auto save/recover does not function.
    Hence the diving into VBA

  21. #21
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    846

    Re: Macro that saves workbook every 4 minutes

    ok, now im frustrated.
    I dont get the F9, F5, F8 parts, im a novice on VBA, picking up as i go along and splicing codes together from various places.
    I dont get how the name is inconsistent with the code? 04-May-2012 Production Schedule. (brackets only used to show the name b4)
    I have ran the macro StopTimer, Ran StartTimer. time set at 15 seconds, doesnt save, still allows me to undo last data entry, still asks me to save when i click close.
    Sry bud.

  22. #22
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    846

    Re: Macro that saves workbook every 4 minutes

    by all means fella, feel free to send a pm shouting abuse if im missing something simple here lol

  23. #23
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    846

    Re: Macro that saves workbook every 4 minutes

    My original code -
    ThisWorkbook
    Please Login or Register  to view this content.
    Module1
    Please Login or Register  to view this content.
    Is it possible to modify this to work how i need, at least then i can stop driving you insane with questions!!

  24. #24
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Macro that saves workbook every 4 minutes

    I'm looking into it, give me a few minutes.

  25. #25
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Macro that saves workbook every 4 minutes

    4 minutes to be exact

  26. #26
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    846

    Re: Macro that saves workbook every 4 minutes

    timing

  27. #27
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Macro that saves workbook every 4 minutes

    Put this in ThisWorkbook module:

    Please Login or Register  to view this content.
    Put this in a normal module, for example Module1:

    Please Login or Register  to view this content.
    That seems to be working fine here, it saved and all. Now I'm waiting another 4 minutes to see if it will have the error you mentioned about it reopening once it had been closed, but I'm confident it won't. I hope that helps

  28. #28
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    846

    Re: Macro that saves workbook every 4 minutes

    @ Pichingualas - That seems to have resolved both my idiocy and the original issue.
    Many thanks fella, really appreciated. (4 mins late )
    @ Chippy - Thansk alot fella for your input, greatly appreciated.

    Thanks all.

  29. #29
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Macro that saves workbook every 4 minutes

    Always glad to help. Tell me if you have any other issues with this, cause I've known the Schedule:=False method does not always work right away.
    Last edited by Pichingualas; 05-04-2012 at 06:16 PM.

  30. #30
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    846

    Re: Macro that saves workbook every 4 minutes

    Apologies for re-opening this, can i change
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    Both the 'Call GetSavesStarted' & 'Call GetSaves_Started' run on a timer.
    or do i need to add something else?

+ 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