Consulting

Results 1 to 9 of 9

Thread: Solved: Schedule a macro

  1. #1
    VBAX Regular
    Joined
    Sep 2007
    Posts
    20
    Location

    Solved: Schedule a macro

    Hi,

    In a book that will be permanently open, I need to schedule a macro to run each 30 minutes between two specifics hours (6 am and 11 pm).
    Thanks and sorry for my english.

    Cactus.-

  2. #2
    Administrator
    2nd VP-Knowledge Base
    VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Hi Cactus,

    I would check out Chip Pearson's website to learn about the .OnTime method.

    I believe the following code will do what you ask. All you have to do is remove the comments from the following lines:
    [VBA]'Application.OnTime EarliestTime:=dblRunWhen, _
    ' Procedure:=strRunWhat, _
    ' Schedule:=True[/VBA]

    And you have to change the variable strRunWhat to whatever the procedure name is that you want to execute.

    Here is the code:
    [VBA]Option Explicit
    Option Private Module

    Const strRunWhat = "The_Procedure_Name"

    Public Sub SetUpMacroSchedule()
    ' Sets up a schedule for a specific macro(s) to be run
    Dim i As Long
    Dim dblCurrentTimeInLoop As Double ' To stop the loop at 11:00 PM
    Dim dblRunWhen() As Double

    ' Set i=1 to start the array counter
    i = 1

    ' Start time at 6:00 AM (relative to the Computer System Time)
    ReDim dblRunWhen(0)
    dblRunWhen(0) = DateSerial(Year(Now()), Month(Now()), Day(Now())) _
    + TimeSerial(6, 0, 0)

    dblCurrentTimeInLoop = dblRunWhen(0)
    ' FOR TESTING PURPOSES
    Debug.Print DateSerial(Year(dblRunWhen(0)), Month(dblRunWhen(0)), Day(dblRunWhen(0))) _
    + TimeSerial(Hour(dblRunWhen(0)), Minute(dblRunWhen(0)), Second(dblRunWhen(0)))

    ' Create a loop to set up the array of timed tasks.
    ' Loop's condition is to continue looping until the time
    ' of dblCurrentTimeInLoop is greater than 11:00 PM
    Do While (TimeSerial(Hour(dblCurrentTimeInLoop), _
    Minute(dblCurrentTimeInLoop), 0) < TimeSerial(23, 0, 0))
    ReDim Preserve dblRunWhen(i)
    ' Set the next dblRunWhen to the next half-hour
    dblRunWhen(i) = dblRunWhen(i - 1) + TimeSerial(0, 30, 0)

    ' FOR TESTING PURPOSES
    Debug.Print DateSerial(Year(dblRunWhen(i)), Month(dblRunWhen(i)), Day(dblRunWhen(i))) _
    + TimeSerial(Hour(dblRunWhen(i)), Minute(dblRunWhen(i)), Second(dblRunWhen(i)))

    'Application.OnTime EarliestTime:=dblRunWhen, _
    ' Procedure:=strRunWhat, _
    ' Schedule:=True

    dblCurrentTimeInLoop = dblRunWhen(i)

    ' Increment i
    i = i + 1

    Loop

    End Sub[/VBA]
    As it is now, it will print the schedule in the Immediate window for you (just to be sure that the code works correctly). Test it and let us know if it produces the correct schedule for you

    Good luck!




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  3. #3
    VBAX Regular
    Joined
    Sep 2007
    Posts
    20
    Location
    It works perfectly!!!

    Thank you very much

  4. #4
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Your welcome

    And welcome to VBAX!




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  5. #5
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Oh yeah. Don't forget to mark your thread "Solved" by using the thread tools inside the thread




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  6. #6
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    One large caveat of this is you must have the file open. As you say the file will always be open, this could lead to problems. Such as what if the file closes? If this would ever be an issue for you, setup a vbs file to run with Windows Scheduler (assuming you have a Windows OS) at your desired times. An example of this can be found at this running thread ...

    http://forums.techguy.org/business-a...xcel-help.html

    HTH

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    If it isn't open, it will open when it's time is due, as long as Excel is running.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    VBAX Regular
    Joined
    Sep 2007
    Posts
    20
    Location
    Hi,

    I`ll try to use the Windows Scheduler and an AutoOpen maybe (?)...It looks easiest and strongest for a beginer like me.
    Anyway the solution porpoused by malik641 was very useful.

    Thanks again

  9. #9
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Quote Originally Posted by xld
    If it isn't open, it will open when it's time is due, as long as Excel is running.
    Hence the possible problem.

    If it is a non-issue to the OP, kudos, but it needs to be addressed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •