Excel

Schedule a Macro to Run After a Specified Period of Time

Ease of Use

Easy

Version tested with

2002 

Submitted by:

Jacob Hilderbrand

Description:

This macro demonstrates how to schedule a macro to run after a set amount of time. 

Discussion:

You want to run a macro, but not right now. You want it to run in 10 seconds, or 20 minutes, or 7 hours. This macro demonstrates how to do just that. 

Code:

instructions for use

			

Option Explicit Sub ScheduleAProcedure() 'TimeSerial(hours, minutes, seconds) Application.OnTime Now + TimeSerial(0, 0, 10), "TimedMacro" End Sub Sub TimedMacro() 'This should be the macro that runs; we simply show a message box MsgBox "The code was just executed!", vbInformation End Sub

How to use:

  1. Open Excel.
  2. Alt + F11 to open the VBE.
  3. Insert Module.
  4. Paste the code there.
  5. Change the TimeSerial line as needed. The syntax is: TimeSerial(Hours, Minutes, Seconds). This is the amount of time that will pass before "TimedMacro" will run.
  6. Close the VBE (Alt + Q or press the X in the top right corner).
 

Test the code:

  1. Tools | Macro | Macros...
  2. Select ScheduleAProcedure and press Run.
 

Sample File:

TimeMacro.ZIP 6.11KB 

Approved by mdmackillop


This entry has been viewed 414 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express