How to schedule an Excel macro to run automatically (and what breaks)
Run an Excel VBA macro on a schedule with Windows Task Scheduler and a small script, the settings that matter, and the problems that stop unattended Excel runs.
In short: Excel can't schedule itself. Use Windows Task Scheduler to start a small script at the set time; the script opens the workbook, runs the macro, saves and closes Excel. Run the task as a user who is signed in ("Run only when user is logged on"), handle pop-ups inside the macro, and write a log so you know whether it worked.
We run Excel macros on a schedule for our own automation work, and most of the problems below are ones we hit ourselves. If you'd rather not keep a Windows machine running, our post on running Excel VBA macros in the cloud covers the other route.
What you need
- A Windows computer that is on at the scheduled time, with Excel installed.
- The workbook with the macro (a .xlsm file).
- A short script that starts Excel and runs the macro.
- Windows Task Scheduler (built in).
Step 1: make the macro safe to run unattended
Before scheduling, change the macro so it never waits for a person:
Application.DisplayAlerts = False ' no "Do you want to save?" prompts
Application.ScreenUpdating = False
On Error GoTo Failed
' ... your work ...
Exit Sub
Failed:
' write the error to a log sheet or text file instead of showing a message box
Remove MsgBox and InputBox calls, or put them behind a flag. A single message box is enough to make a scheduled run hang forever.
Step 2: write the launcher script
Save this as run-macro.vbs next to the workbook and change the path and macro name:
Dim xl, wb
Set xl = CreateObject("Excel.Application")
xl.Visible = False
xl.DisplayAlerts = False
Set wb = xl.Workbooks.Open("C:\Reports\Weekly.xlsm")
xl.Run "'Weekly.xlsm'!Module1.BuildReport"
wb.Save
wb.Close False
xl.Quit
Run it once by double-clicking. If the report updates, the script works.
Step 3: create the scheduled task
- Open Task Scheduler and choose Create Task (not "Basic Task").
- General: give it a name, and choose Run only when user is logged on.
- Triggers: New, set the schedule (daily at 6:00, every Monday, and so on).
- Actions: New, Program
wscript.exe, Arguments"C:\Reports\run-macro.vbs". - Settings: tick Stop the task if it runs longer than and set a sensible limit, such as one hour.
- Save, then right-click the task and Run to test it.
Why "Run only when user is logged on"?
Excel is a desktop program. Microsoft does not support or recommend running Office applications unattended from a non-interactive account or service (its guidance on server-side automation of Office explains why): dialogs have nowhere to appear, printers and add-ins may be missing, and Excel can hang. Running in a signed-in user's session avoids most of this. The computer can be locked; the user must be signed in.
What usually breaks
- A hidden dialog. "Update links?", "Enable content?", "File in use". Set
DisplayAlerts = False, open withUpdateLinks:=0, and trust the folder in Excel's Trust Center. - The file is open somewhere else. A colleague has it open, or a cloud sync tool is holding it. Copy the workbook to a working folder first, or check for the lock file (
~$Weekly.xlsm). - A leftover Excel process. A previous run crashed and left
EXCEL.EXErunning. The launcher should time out and the task's time limit will stop it. - The PC went to sleep. Tick Wake the computer to run this task under Conditions, and check power settings.
- The user signed out. No session, no Excel.
Know whether it ran
Add a line at the end of the macro that writes the date, time and result to a log file, or emails the result. Silent failures are the real risk of any scheduled job; you want to hear about a failed run the same day.
Frequently asked questions
Can I run an Excel macro without opening Excel?
No. VBA runs inside Excel. A script can open Excel invisibly, run the macro and close it, which looks the same from the outside.
Can Task Scheduler run the macro when no one is logged in?
You can configure it, but Excel often hangs or fails in that mode because Office isn't designed to run without a user session. Keep a user signed in, with the screen locked.
Can I use PowerShell instead of VBScript?
Yes. PowerShell can create the Excel.Application COM object the same way. VBScript is shown here because it needs no setup.
How do I run the macro on several files?
Loop over the files in the script or the macro, and process one at a time. Log each file's result so one failure doesn't hide the others.
-
How to automate a weekly Excel report (from easy wins to fully hands-off)
Four levels of automating a recurring Excel report, from Power Query refresh to a scheduled job that downloads the data, builds the workbook and emails it, with the checks each level needs.
-
Running real Excel VBA macros in the cloud (without rewriting them)
How I made a bookkeeping team's own VBA macro run unattended on a cloud Windows VM that starts when there's work and switches itself off after: the setup and the problems that took longest to fix.
-
How to automate daily POS sales reports for restaurants and multi-store businesses
Stop downloading the same point-of-sale report for every store every morning: what can be automated, how a hands-off daily sales report works, and the checks that keep it reliable.