+ Reply to Thread
Results 1 to 17 of 17

Stopping Timed Macro on Workbook Close

  1. #1
    Registered User
    Join Date
    08-15-2017
    Location
    Isle of Man
    MS-Off Ver
    7.0
    Posts
    25

    Exclamation Stopping Timed Macro on Workbook Close

    I have a workbook that runs a timed macro.

    However whilst I have other Workbooks open this Macro continues to run.

    How to you stop any macros on Workbook?

    I have tried this but cant get it to work :-





    Please Login or Register  to view this content.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Stopping Timed Macro on Workbook Close

    That's only going to stop the time macro when you close the workbook it's in.

    When exactly don't you want the timed macro to run?
    If posting code please use code tags, see here.

  3. #3
    Valued Forum Contributor
    Join Date
    03-24-2014
    Location
    England
    MS-Off Ver
    Excel 2003 - 2016
    Posts
    575

    Re: Stopping Timed Macro on Workbook Close

    This bit:

    Please Login or Register  to view this content.
    Sets the kick off time to the time now + 50 minutes. ie, if you run this at 1pm, the kick off time will be set to 1:50pm.

    To turn it off you need to specify the exact time it was scheduled, ie 1:50pm. You're using the same command, ie whatever time it is closed + 50 minutes. When you kick the timer off you need to record the time it's going to run somewhere so you can use the same time when you turn it off.

  4. #4
    Registered User
    Join Date
    08-15-2017
    Location
    Isle of Man
    MS-Off Ver
    7.0
    Posts
    25

    Re: Stopping Timed Macro on Workbook Close

    Thank you for your quick responses

    The macro is to run at 50 min intervals from opening.

    But the close of the workbook is the point at which this should stop. It could be at anytime within the day.

  5. #5
    Valued Forum Contributor
    Join Date
    03-24-2014
    Location
    England
    MS-Off Ver
    Excel 2003 - 2016
    Posts
    575

    Re: Stopping Timed Macro on Workbook Close

    Again, you can only stop it if you specify the time it is supposed to run.

    Imagine it's a train. The 13:50 from Waterloo.

    If you put in a cancel notification you're not cancelling the 14:50 from waterloo, you're cancelling the 13:50 from waterloo.....and you need to tell Excel that it's the 13:50 from waterloo you're cancelling, not any other trains from waterloo that might arrive at a different time.


    ie. When you first run the application.ontime and tell Excel that at Now + 50 mins (eg 13:50) run the macro called xxxxxx. You MUST record the now + 50 mins time somewhere so that you can use the EXACT SAME TIME when you remove the scheduling on exit.

    geddit now?

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Stopping Timed Macro on Workbook Close

    Try something like this.
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    08-15-2017
    Location
    Isle of Man
    MS-Off Ver
    7.0
    Posts
    25

    Re: Stopping Timed Macro on Workbook Close

    Thanks Norie

    I have tried that one, unfortunately the workbook_before close element errors.

    Runtime error 1004

    Method 'OnTime' of object '_Application' failed

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Stopping Timed Macro on Workbook Close

    Try declaring tmExit as Public in a standard module (Insert>Module).
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    08-15-2017
    Location
    Isle of Man
    MS-Off Ver
    7.0
    Posts
    25

    Re: Stopping Timed Macro on Workbook Close

    Unfortunately the same error. Im running an autosave ( which I believe is generally frowned upon )

    As noted before its just that if I have another excel document open but close the one running the "SaveThis" it keeps trying to run although the parent workbook is closed

  10. #10
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: Stopping Timed Macro on Workbook Close

    .
    Post all of your Timer code for review please.

  11. #11
    Registered User
    Join Date
    08-15-2017
    Location
    Isle of Man
    MS-Off Ver
    7.0
    Posts
    25

    Re: Stopping Timed Macro on Workbook Close

    In 'ThisWorkbook'

    Please Login or Register  to view this content.
    In 'Module1' :-

    Please Login or Register  to view this content.
    In 'Module2'

    Please Login or Register  to view this content.
    In 'Module3'

    Please Login or Register  to view this content.

  12. #12
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Stopping Timed Macro on Workbook Close

    Hi,

    Did you manually run the Workbook_Open code after you made the changes? If not, you'd get an error the first time you try to close the workbook, as the time variable wasn't set.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  13. #13
    Registered User
    Join Date
    08-15-2017
    Location
    Isle of Man
    MS-Off Ver
    7.0
    Posts
    25

    Re: Stopping Timed Macro on Workbook Close

    Hi,

    Thank you, not sure what I was doing wrong but it would seem to be OK now.

    Only issue is that the code was to run every 50 minutes from the time of opening. The code only seems to run this once ?

    Please Login or Register  to view this content.

  14. #14
    Valued Forum Contributor
    Join Date
    03-24-2014
    Location
    England
    MS-Off Ver
    Excel 2003 - 2016
    Posts
    575

    Re: Stopping Timed Macro on Workbook Close

    Well it would do. The application ontime isn't a repeating scheduler. It schedules one run of the macro specified at the specified time.

    In order to run it again add the same code to the macro that runs, logging the new scheduled run time so you can turn it off on close.

  15. #15
    Registered User
    Join Date
    08-15-2017
    Location
    Isle of Man
    MS-Off Ver
    7.0
    Posts
    25

    Re: Stopping Timed Macro on Workbook Close

    Thank you ,

    As I am a bit of a novice , it is only this bit ( being that the other parts are turning things on and off essentially ) ?
    Please Login or Register  to view this content.

  16. #16
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Stopping Timed Macro on Workbook Close

    No, you need to add the OnTime as well
    Please Login or Register  to view this content.
    to the end of the SaveThis routine.

  17. #17
    Registered User
    Join Date
    08-15-2017
    Location
    Isle of Man
    MS-Off Ver
    7.0
    Posts
    25

    Re: Stopping Timed Macro on Workbook Close

    Thank you all , managed to sort that with a bit of trial and erro. Obviously using the full bit of code.

    Please Login or Register  to view this content.
    Added to the Macro as noted

+ 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. [SOLVED] Issue with Open, copy, close Macro - when trying to close workbook Clipboard error
    By seanpcorbett1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-13-2016, 05:14 PM
  2. Stopping Clock Macro on Close
    By bvercher in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-30-2014, 03:22 PM
  3. [SOLVED] Need help stopping auto-close macro for work file! Please help!
    By brolsen in forum Excel General
    Replies: 2
    Last Post: 02-21-2014, 03:44 PM
  4. How to open,save and close another workbook from the current workbook using macro?
    By ravikumar00008 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-15-2012, 10:42 AM
  5. Timer and timed close of a spreadsheet
    By jeffwest2 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-20-2009, 06:57 AM
  6. Stop timed macro on close
    By Allardin in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-26-2008, 12:11 AM
  7. Help .. stopping Application.Ontime when you close a workbook
    By L2B in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-13-2005, 06:43 AM

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