A VBA weekly report generator is a macro-driven workflow inside one Excel workbook that pulls raw timekeeping entries, calculates weekly hours, and outputs a payroll-ready spreadsheet in under 30 seconds. Instead of copy-pasting timesheet data for a 12-person team every Friday, you write a single module, press F5, and your paycheck columns populate automatically. Below is the exact setup, code, and troubleshooting path for a US small-business owner or office manager running 5 to 50 employees on a legacy Excel spreadsheet.
Set Up Your Workbook Skeleton Before Writing a Single Line of VBA
Before you touch the VBA editor, your workbook needs a predictable three-sheet structure. Sheet 1 holds raw timekeeping rows (employee name, Monday-through-Friday hours, overtime flag). Sheet 2 is the weekly report that your macro fills. Sheet 3 is a reference table mapping each employee to their hourly rate and department. Name the sheets exactly Raw_Time, Weekly_Report, and Employee_Rates so your module code references them by name instead of fragile tab positions.
Open the VBA editor with Alt + F11, insert a standard module under Microsoft Excel Objects in the VBAProject tree, and name it modWeeklyReport. This module will hold every sub and function you build. Keep the workbook file under 500 KB so it loads quickly on a standard office laptop.
- Workbook structure: Create three sheets with the exact tab names above. Add a fourth sheet called
Settingswhere you store the pay-period start date and the company name that appears in the report header. - Module placement: In the VBA Project Explorer, right-click the new module and rename it
modWeeklyReport. All your subs go here; avoid putting code in theSheet1module so the macro is independent of tab order. - Named ranges: On the
Employee_Ratessheet, select the rate column and create a defined name calledHourlyRates. Select the employee names and name that rangeEmployeeNames. Your macro will reference these names instead of hard-coded addresses.
Write the Core Macro Loop That Pulls Timesheet Rows Into Your Weekly Report
The heart of your VBA weekly report generator is a single For loop that iterates over every populated row on Raw_Time, sums the five day-columns, flags overtime when the total exceeds 40 hours, and writes the result to the next open row on Weekly_Report. For a 1Γ5 range, simple addition of the five day values is faster than calling any built-in worksheet function, so a handful of CDbl conversions and a single total line keep the loop lean.
Below is the working sub. Paste it into modWeeklyReport, make sure your raw data starts at row 2 (row 1 holds headers), and run it with F5. The loop typically finishes in roughly 1β2 seconds for 50 employees.
Sub BuildWeeklyReport()
Dim wsRaw As Worksheet, wsRep As Worksheet
Dim lastRow As Long, r As Long, nextRow As Long
Dim hrsMon As Double, hrsTue As Double, hrsWed As Double
Dim hrsThu As Double, hrsFri As Double
Dim tot As Double, ot As Double
Set wsRaw = ThisWorkbook.Sheets("Raw_Time")
Set wsRep = ThisWorkbook.Sheets("Weekly_Report")
lastRow = wsRaw.Cells(wsRaw.Rows.Count, 1).End(xlUp).Row
nextRow = wsRep.Cells(wsRep.Rows.Count, 1).End(xlUp).Row + 1
For r = 2 To lastRow
hrsMon = CDbl(wsRaw.Cells(r, 2).Value)
hrsTue = CDbl(wsRaw.Cells(r, 3).Value)
hrsWed = CDbl(wsRaw.Cells(r, 4).Value)
hrsThu = CDbl(wsRaw.Cells(r, 5).Value)
hrsFri = CDbl(wsRaw.Cells(r, 6).Value)
tot = hrsMon + hrsTue + hrsWed + hrsThu + hrsFri
ot = IIf(tot > 40, tot - 40, 0)
' Write all seven columns in one Range.Value call
wsRep.Range("A" & nextRow).Resize(1, 7).Value = Array(wsRaw.Cells(r, 1).Value, hrsMon, hrsTue, hrsWed, hrsThu, hrsFri, ot)
nextRow = nextRow + 1
Next r
End Sub
- Why
CDblnotVal:CDblthrows a type error if a cell is empty, forcing you to fix the source data.Valsilently returns 0 and can hide the problem. - Write block: Use
wsRep.Range("A" & nextRow).Resize(1, 7).Value = Array(...)to write all seven columns in one call. This is significantly faster than seven individual.Cellswrites.
Map Your Payroll Output Columns So Paycheck and Paystub Data Align
Once the loop writes hours and overtime into Weekly_Report, you still need the dollar figures. Column H should hold gross pay (regular hours Γ rate + overtime hours Γ 1.5 Γ rate). Column I holds the net paystub figure after a flat 22% placeholder for federal withholding so your accountant can plug in the real schedule. Column J stores the pay-period date range (e.g., "Mar 4 β Mar 10, 2025") pulled from the Settings sheet.
Format the entire data block as #,##0.00 so paycheck amounts display in dollars and cents. Set column widths to 12 for dollar columns and 18 for the name column so the printout on a standard 8.5 Γ 11-inch sheet fits without spilling into a second page. Freeze the top row so the header stays visible when a 50-row team scrolls.
- Gross pay formula (in-cell): = (E2 * VLOOKUP(A2, EmployeeNames, 2, FALSE)) + (F2 * VLOOKUP(A2, EmployeeNames, 2, FALSE) * 1.5)
- Paystub placeholder: = H2 * 0.78. Replace 0.78 with your actual net-to-gross ratio once your payroll provider confirms the withholding tier.
- Print area: Select A1:J51, then
Page Layout β Print Area β Set Print Area. This keeps the weekly report on a single page for the file cabinet.
Protect Your Friday Workflow: Error Handling, Backups, and File Naming
Running a macro that rewrites payroll data without a safety net is how a bad weekend starts. Add On Error GoTo ErrorHandler at the top of every sub and log the error number, description, and timestamp to a dedicated Error_Log sheet. Never use 'On Error GoTo ErrorHandler' with proper error logging in a payroll or accounting scriptβit silently swallows errors such as a 1004 (object definition) and writes zeros into your paycheck columns.
Before the macro runs, copy the workbook to a timestamped backup. The filename follows the pattern Payroll_Wk04_2025-01-10.xlsx. If the macro fails halfway through Weekly_Report, you restore the backup and re-run. You cannot undo a macro with Ctrl+Z; the Undo stack clears the instant VBA executes.
- Backup sub: Create a second sub called
BackupBeforeRunthat callsThisWorkbook.SaveCopyAswith aFormat(Now, "yyyy-mm-dd")suffix. - Error handler:
ErrorHandler:β writeErr.Number,Err.Description, andNowto the log sheet, thenMsgBoxwith a clear instruction to restore the backup. - Macro Security: Set the workbook to "Trust this publisher" via the Trust Center so the F5 keypress does not trigger a yellow warning banner every Friday.
Fix the Five Most Common VBA Mistakes When Automating Payroll Reports
Many beginners hit the same wall in their first two weeks. The fixes below address a large share of the support tickets that small-business office managers send to their IT help desk.
- Screen flicker: Wrap the entire sub in
Application.ScreenUpdating = FalseandApplication.Calculation = xlCalculationManual. Reset both toTrueat the end of the sub (or in a cleanup block). Without this, 50 rows of writes cause visible lag that can stretch past 40 seconds. - Wrong sheet reference: If you typed
Sheets(1)instead ofSheets("Raw_Time"), the macro reads the wrong tab the moment someone reorders sheets. Always reference by name. - Excel for the Web: VBA does not run in the browser-based version of Excel. If your team accesses the workbook through Office 365 on a phone, the macro button simply will not appear. Pin the desktop app for anyone who needs to run the generator.
- Locked cells: If you protect the
Weekly_Reportsheet to prevent hand-edits, unprotect it before the macro writes. AddwsRep.Unprotectat the top andwsRep.Protectat the bottom of the sub. - Missing reference: If your code uses
DateTimeformatting functions from a separate module, add theMicrosoft Visual Basic for Applicationsreference via Tools β References in the VBA editor. A missing reference causes a compile error that halts the entire payroll run before it executes.
| Approach | One-Time Setup | Weekly Runtime (50 Emp) | Flexibility If Layout Changes |
|---|---|---|---|
| Manual copy-paste into a template | 2 hours | 45β90 min per week | High β you rebuild the paste zone by hand |
| Pure VBA macro (this guide) | 3β4 hours | < 30 seconds | Medium β edit the module code |
| Power Query + VBA hybrid | 5β6 hours | ~10 seconds (refresh) | High β Query re-maps columns automatically |
| VBA with named ranges & data validation | 4β5 hours | < 30 seconds | Medium β named ranges must be re-mapped |
Frequently Asked Questions
Does a VBA weekly report generator work in every version of Excel on Windows and Mac?
Yes, VBA is supported in the desktop Excel application. For the features used in this guide, 2016 or later on Windows and 2011 or later on Mac are safe minimums. Excel for the Web in a browser does not support VBA, so team members who only use the online viewer cannot trigger the macroβthey need the installed desktop app.
Can this workflow scale to a 50-employee team without rewriting the code?
Yes. The loop reads to the last populated row on Raw_Time, so adding rows from employee 13 through 50 requires no code changes. You only update the Employee_Rates lookup table with new names and hourly rates. Runtime stays under 30 seconds for 50 rows because the write block is a single Range.Value assignment rather than 50 individual cell calls.
What happens if an employee's timesheet format changes mid-week, for example adding a Saturday column?
The macro will read the new column only if you update the hard-coded column letters in the loop (currently B through F for MondayβFriday). Add a cols variable that counts populated columns on Raw_Time row 1, then loop that count instead of a fixed range. This takes two extra lines of code and keeps the generator resilient to layout shifts without a full rewrite.