Excel VBA Schedule Report: Automate Your Reporting
In this article
You can automate a recurring Excel schedule report with VBA by writing a macro that opens your source workbook, copies the relevant data into a formatted report sheet, saves the file with a date-stamped name, and optionally emails it. The key is to build the macro once, then trigger it manually or on a schedule using Windows Task Scheduler. This guide walks through each step with practical, tested code patterns.
What You'll Need
- Microsoft Excel (desktop version for Windows or Mac) with macros enabled.
- A source workbook containing the data you want to report on (e.g., a log, a database export, or a sheet updated by others).
- Basic familiarity with the VBA editor (ALT+F11) and saving files as .xlsm.
- For scheduled runs: Windows Task Scheduler (Windows) or a calendar reminder to run manually (Mac).
Step-by-Step
- Prepare your workbooks. Create two files: one source workbook (e.g.,
SourceData.xlsx) and one report template (e.g.,ReportTemplate.xlsm). In the template, design a sheet namedReportwith headers and formatting. Save the template as.xlsm(macro-enabled). - Open the VBA editor. In the template workbook, press ALT+F11. In the Project Explorer, right-click
VBAProject (ReportTemplate.xlsm)and choose Insert > Module. This creates a blank module where you'll paste code. - Write the core macro. Paste the following code, adjusting file paths and sheet names to match your setup:
Sub GenerateScheduleReport() Dim srcWb As Workbook Dim repWs As Worksheet Dim lastRow As Long Dim reportDate As String ' Open source workbook (read-only to avoid locks) Set srcWb = Workbooks.Open("C:\Reports\SourceData.xlsx", ReadOnly:=True) ' Set reference to report sheet in this workbook Set repWs = ThisWorkbook.Sheets("Report") ' Clear previous data (keep headers in row 1) repWs.Range("A2:Z10000").ClearContents ' Find last row in source (assuming data on Sheet1, column A) lastRow = srcWb.Sheets("Sheet1").Cells(srcWb.Sheets("Sheet1").Rows.Count, "A").End(xlUp).Row ' Copy data (adjust range as needed) srcWb.Sheets("Sheet1").Range("A2:Z" & lastRow).Copy Destination:=repWs.Range("A2") ' Close source without saving srcWb.Close SaveChanges:=False ' Add report date in a cell (e.g., B1) repWs.Range("B1").Value = "Report Date: " & Format(Date, "yyyy-mm-dd") ' Save report with date-stamped filename reportDate = Format(Date, "yyyy-mm-dd") ThisWorkbook.SaveAs "C:\Reports\ScheduleReport_" & reportDate & ".xlsx", FileFormat:=xlOpenXMLWorkbook End Sub - Test the macro manually. Press F5 while in the code or run it from Developer > Macros. Check that the report sheet fills correctly and the file saves. If you get a "file not found" error, verify the path in the
Workbooks.Openline. - Add optional emailing (Outlook only). If you use desktop Outlook, append this code before the
End Subto email the report:
Note: Outlook must be configured and running. This will not work with webmail or Outlook on the web.Dim OutApp As Object, OutMail As Object Set OutApp = CreateObject("Outlook.Application") Set OutMail = OutApp.CreateItem(0) With OutMail .To = "recipient@example.com" .Subject = "Schedule Report " & reportDate .Body = "Please find the attached schedule report." .Attachments.Add ThisWorkbook.FullName .Send End With Set OutMail = Nothing Set OutApp = Nothing - Save and close the VBA editor. Save the template workbook as
.xlsm. Close Excel to release file locks. - Schedule the macro with Windows Task Scheduler. Open Task Scheduler, create a basic task, set the trigger (e.g., daily at 8 AM), and for the action choose Start a program. Browse to your Excel executable (e.g.,
C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE) and add the argument"C:\Reports\ReportTemplate.xlsm" /e(the/eflag prevents the startup screen). Alternatively, create a small .vbs script that opens the workbook and runs the macro, then schedule that script. Test the scheduled task by running it manually. - Verify the output. After the scheduled run, check that a new file appears in
C:\Reports\with the correct date stamp and that the data matches the source. If emailing, confirm the message was sent.
Common Mistakes
- Hardcoding paths incorrectly. Use double backslashes in VBA strings or use
Environ("USERPROFILE")for portable paths. A single backslash causes errors. - Forgetting to disable screen updating and alerts. Add
Application.ScreenUpdating = FalseandApplication.DisplayAlerts = Falseat the start, and set them back toTrueat the end. This speeds up the macro and prevents "overwrite?" prompts. - Leaving source workbook open. The macro opens it read-only, but if someone else has it open exclusively, the open will fail. Use
ReadOnly:=Trueas shown. - Saving as .xlsx from a macro-enabled workbook. The
SaveAsline usesxlOpenXMLWorkbookwhich saves as .xlsx; that's fine for the report, but the template itself must remain .xlsm. Do not save over the template. - Not handling errors. Add
On Error GoTo Cleanupand a cleanup label to close workbooks and re-enable settings even if an error occurs.
Tips
- Use
ThisWorkbook.Pathto build relative paths, making the macro portable across folders. - Name your report file with a timestamp (including time) if you run multiple times per day.
- Test the macro with a copy of your data first to avoid accidental overwrites.
- For Mac users, Task Scheduler is not available; use a calendar reminder to run the macro manually, or explore third-party automation tools that support AppleScript.
- Keep a backup of your template before making changes.
Troubleshooting
If the macro does not run when scheduled, first check that the task's "Start in" folder is set to the folder containing the workbook. Second, ensure Excel is not already open with a dialog box. Third, verify that macros are enabled in the Trust Center (File > Options > Trust Center > Trust Center Settings > Macro Settings). If you get a "Permission denied" error, the file may be open by another user or marked as read-only. If the email step fails, confirm Outlook is installed and the default profile is configured. As a last resort, add MsgBox Err.Description in an error handler to see the exact message.
Frequently Asked Questions
Can I run this on Excel for Mac?
Yes, the VBA code works on Mac Excel, but Windows Task Scheduler is not available. You'll need to run the macro manually or use a Mac automation tool like Automator with AppleScript.
Do I need Outlook for emailing?
The email code uses Outlook's desktop application. It will not work with Outlook on the web or other email clients. You can omit the email section if not needed.
How do I avoid overwriting the previous report?
The SaveAs line includes the date in the filename, so each day's report is separate. If you run multiple times per day, add the time using Format(Now, "yyyy-mm-dd_hhmm").
What if the source data has more columns than my report?
Adjust the copy range in the macro (e.g., "A2:Z") to match your actual data extent. Copying extra columns is harmless but may slow things down.
Can I schedule this without Task Scheduler?
On Windows, Task Scheduler is the standard built-in tool. You could also use a third-party scheduler, but avoid unofficial or cracked software. On Mac, use Calendar reminders or Automator.