+ Reply to Thread
Results 1 to 5 of 5

Need code to run macro in excel every 15 minutes, but only if user has File open

Hybrid View

  1. #1
    Registered User
    Join Date
    05-07-2013
    Location
    Ireland
    MS-Off Ver
    Excel 2003/2010
    Posts
    7

    Exclamation Need code to run macro in excel every 15 minutes, but only if user has File open

    I'm working on a project to update production notes in a manufacturing schedule. I've a macro written to pull in the data to the manufacturing schedule, but I need to execute the macro in the file automaticaly every 16 minutes, but only while the user has the manufacturing schedule open.

    I've tried using "Application.OnTime" with specific times, but this seems to open the manufacturing schdule if its closed.
    A point that may also be relevant is that this will be a shared file on Excel 2010.

    I'm pretty new to VBA so any assistance with the code would be much appreciated. The code I'm using is pretty basic:

    Private Sub Workbook_Open()
    
    'Application.OnTime TimeValue("08:00:30"), "Open_SFCDB"
    'Application.OnTime TimeValue("08:16:30"), "Open_SFCDB"
    'Application.OnTime TimeValue("08:32:30"), "Open_SFCDB"
    'Application.OnTime TimeValue("08:48:30"), "Open_SFCDB"
    'Application.OnTime TimeValue("09:04:30"), "Open_SFCDB"
    ......Etc, Etc for 24 hr period
    
    'End Sub

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Need code to run macro in excel every 15 minutes, but only if user has File open

    your open event should schedule the macro once only storing the run time in a public variable. the macro should then update the run time for the next interval and reschedule itself. the beforeclose event of the workbook should then cancel the scheduled run using the final parameter of Ontime
    Private Sub Workbook_Open()
    RunWhen = now() + timeserial(0, 0, 30)
    Application.OnTime runwhen, "Open_SFCDB"
    End Sub
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Application.OnTime runwhen, "Open_SFCDB", , False
    end sub
    Normal module
    
    Public RunWhen as Date
    
    Sub Open_SFCDB()
    ' existing code here
    
    RunWhen = now() + timeserial(0, 16, 0)
    Application.OnTime runwhen, "Open_SFCDB"
    End Sub
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Registered User
    Join Date
    05-07-2013
    Location
    Ireland
    MS-Off Ver
    Excel 2003/2010
    Posts
    7

    Re: Need code to run macro in excel every 15 minutes, but only if user has File open

    [QUOTE=JosephP;3241610]your open event should schedule the macro once only storing the run time in a public variable. the macro should then update the run time for the next interval and reschedule itself. the beforeclose event of the workbook should then cancel the scheduled run using the final parameter of Ontime

    Hi JosephP, Thanks so much for your response, this worked brilliantly.

  4. #4
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Need code to run macro in excel every 15 minutes, but only if user has File open

    Review this first:

    http://www.cpearson.com/Excel/OnTime.aspx

    If you schedule the periodic activity one event at a time, you can call StopTimer() from a Workbook close macro.
    Gary's Student

  5. #5
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Need code to run macro in excel every 15 minutes, but only if user has File open

    you're welcome :-)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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