Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA Schedule Macro: How to Automate Tasks

· 6 min read · Home Automation

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To schedule an Excel VBA macro, you can use the Application.OnTime method for tasks that run while the workbook is open, or combine a VBA macro with Windows Task Scheduler for tasks that must run automatically at specific times regardless of whether the file is open. The Application.OnTime method is the native way to tell Excel to execute a specific procedure at a future date and time, making it ideal for reminders, periodic data refreshes, or timed alerts.

What You'll Need

  • Microsoft Excel installed on a Windows PC.
  • A workbook saved in the .xlsm (Excel Macro-Enabled Workbook) or .xlsb (Excel Binary Workbook) format.
  • Basic familiarity with the Visual Basic for Applications (VBA) editor.
  • Administrator privileges on your computer if you intend to use Windows Task Scheduler.

Step-by-Step

Depending on your goal, you will choose one of two primary methods. Method A is for internal scheduling (workbook must be open), and Method B is for external scheduling (workbook opens and runs automatically).

Method A: Using Application.OnTime (Internal Scheduling)

  1. Open your Excel workbook and press Alt + F11 to open the VBA Editor.
  2. In the Project Explorer on the left, right-click your workbook name and select Insert > Module.
  3. Create the macro you want to run. For example, copy and paste this simple alert macro:
    Sub MyScheduledMacro()
            MsgBox "The scheduled task is now running!"
            ' Call the scheduler again to make it repeat
            ScheduleNextRun
        End Sub
  4. In the same module, create the scheduling procedure. This tells Excel when to trigger the macro. Use the following code:
    Sub ScheduleNextRun()
            ' Schedules the macro to run 10 seconds from now
            Application.OnTime Now + TimeValue("00:00:10"), "MyScheduledMacro"
        End Sub
  5. To ensure the schedule starts as soon as the file opens, double-click ThisWorkbook in the Project Explorer.
  6. Change the dropdown menu at the top left from (General) to Workbook and the right dropdown to Open.
  7. Inside the Private Sub Workbook_Open() event, type Call ScheduleNextRun.
  8. Save the workbook as an .xlsm file, close it, and reopen it to test the automation.

Method B: Using Windows Task Scheduler (External Scheduling)

  1. Open the VBA Editor (Alt + F11) and create the macro you wish to automate in a standard module.
  2. To make the macro run automatically upon opening, double-click ThisWorkbook and use the Workbook_Open event:
    Private Sub Workbook_Open()
            Call MyScheduledMacro
            ThisWorkbook.Save
            Application.Quit
        End Sub
  3. Save and close the Excel file.
  4. Click the Windows Start button, type "Task Scheduler", and press Enter.
  5. In the Actions pane on the right, click Create Basic Task....
  6. Give your task a name (e.g., "Daily Excel Report") and click Next.
  7. Select your trigger frequency (Daily, Weekly, Monthly, etc.) and click Next.
  8. Set the specific start date and time for the task to begin and click Next.
  9. Under "Action", select Start a program and click Next.
  10. In the "Program/script" box, browse to the location of your excel.exe file. This is typically found in C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE (the path may vary slightly depending on your Office version).
  11. In the "Add arguments (optional)" box, type the full file path to your workbook in double quotes, for example: "C:\Users\YourName\Documents\ScheduledReport.xlsm".
  12. Click Next, review the summary, and click Finish.

Common Mistakes

  • Using the wrong file format: Saving a workbook as a standard .xlsx file strips away all VBA code. Always use .xlsm or .xlsb.
  • Hard-coding time values: In Application.OnTime, users often forget that Now + TimeValue("00:00:10") creates a one-time event. To make it a recurring loop, the macro itself must call the scheduling function again before it finishes.
  • Ignoring Security Settings: If "Disable all macros without notification" is selected in the Trust Center, the scheduled macro will not run. You must enable macros or add the folder to "Trusted Locations".
  • Incorrect File Paths: In Windows Task Scheduler, failing to put the workbook path in double quotes will cause the task to fail if there are spaces in the folder or file names.
  • Overlapping Schedules: Setting a macro to run every minute when the macro itself takes two minutes to complete can cause Excel to freeze or crash as it attempts to stack multiple instances of the same procedure.

Tips

  • Use a Global Variable for Cancellation: When using Application.OnTime, it is wise to store the scheduled time in a public variable. This allows you to use Application.OnTime [Variable], "MacroName", Schedule:=False to cancel the timer if the user closes the workbook or wants to stop the process.
  • Error Handling: Always include On Error GoTo ErrorHandler in scheduled macros. Since these run automatically, an unhandled error will pop up a debug window that halts the entire process until a human clicks "OK".
  • Logging: Since scheduled macros often run in the background, write a small routine that logs the date, time, and status of the run to a hidden worksheet. This helps you verify that the macro actually executed while you were away.
  • Avoid MsgBox: When using Windows Task Scheduler, avoid using MsgBox or any interactive prompts. If the macro hits a message box, it will pause indefinitely, keeping Excel open in the background and potentially blocking future scheduled runs.

Troubleshooting

If your scheduled macro does not run, follow these steps in order:

  1. Check Macro Security: Go to File > Options > Trust Center > Trust Center Settings > Macro Settings. Ensure that macros are enabled or that the file is stored in a Trusted Location.
  2. Verify the Workbook Name: In Application.OnTime, the macro name must be passed as a string (e.g., "MyMacro"). If you rename the macro in the module but not in the scheduler, it will fail silently.
  3. Test the Path Manually: For Windows Task Scheduler, copy the exact string you put in the "Arguments" box and paste it into the Windows Run dialog (Win + R). If the file doesn't open, your path is incorrect.
  4. Check Task Scheduler History: In the Task Scheduler library, select your task and click the "History" tab. If it says "Task Completed" but nothing happened in Excel, the issue is likely within the VBA code (e.g., a crash or a security prompt) rather than the Windows trigger.
  5. Restart Excel: Sometimes a "ghost" instance of Excel remains running in the background (visible in Task Manager). This can interfere with new instances of the workbook opening. End all Excel.exe processes and try again.

Frequently Asked Questions

Can I schedule a macro to run while my computer is asleep?

No, Excel requires the system to be awake to execute code. However, in Windows Task Scheduler, you can go to the "Conditions" tab and check "Wake the computer to run this task," though this depends on your PC's hardware and power settings.

Will Application.OnTime work if I am editing a cell?

No. If you are actively typing in a cell (cell edit mode), Excel pauses all VBA execution. The macro will wait until you press Enter or Esc to exit the cell before it triggers.

How do I stop a recurring OnTime macro?

You must call the Application.OnTime method again with the exact same time value that was used to schedule it, but set the Schedule parameter to False.

Does the workbook need to be open for Windows Task Scheduler to work?

No. Windows Task Scheduler launches the Excel application and opens the specified file. If you have a Workbook_Open macro, it will execute immediately upon launch.

Can I schedule a macro to run on a different computer?

The schedule is tied to the local machine. To run a macro on another computer, you must set up the Task Scheduler or the Workbook_Open event on that specific machine, and the file must be accessible via a local or network path.

What happens if the scheduled time passes while the PC is off?

For Application.OnTime, the event is lost. For Windows Task Scheduler, you can go to the "Settings" tab and check "Run task as soon as possible after a scheduled start is missed."