Every Friday, you open the same legacy workbook, copy hours from twelve timesheets, run a dozen SUM and VLOOKUP formulas by hand, and format a paycheck report for each employee. An Excel payroll report generator built with VBA macros collapses that entire routine into a single button click. You define the rules once, and the macro handles the data pull, calculation, and formatted output for every employee on your roster—whether you manage five people or forty-five.
If your small business still relies on a half-broken spreadsheet from 2019 and you spend three or more hours every pay period just getting the numbers right, the setup below will significantly simplify that workflow.
Building Your VBA Payroll Workbook From Scratch
Before writing a single line of VBA code, you need a clean workbook structure. A disorganized sheet layout will make your macro fragile and very difficult to debug later.
- Create a new Excel workbook and rename the sheets:
TIMESHEET,PAYROLL_CALC,PAYSTUB_OUT, andMASTRO(your master employee table). - In
MASTRO, list each employee with columns for ID, name, hourly rate, FTE status, and tax withholding codes. - In
TIMESHEET, set up one block per employee with cells for Mon–Sun hours and an overtime threshold. - In
PAYROLL_CALC, leave cells blank for now—your macro will populate them.
Save the file as a .xlsm so macros are preserved. Keep a dated backup copy in a separate folder before every pay period, because running a VBA macro clears your Undo stack entirely and Ctrl+Z will not reverse anything.
Writing Your First VBA Macro for a Weekly Payroll Tracker
Your first macro should do one job: read the current week's hours from TIMESHEET and write calculated gross pay into PAYROLL_CALC. Open the VBA editor with Alt + F11, insert a standard module, and paste the starter routine below.
- Loop logic: A
For Eachloop over each employee row inMASTROpulls the corresponding timesheet block and sums regular and overtime hours. - Pay calculation: Multiply regular hours by the base rate; multiply OT hours (anything above 40/week under FLSA) by 1.5× rate, then write the total to
PAYROLL_CALC. - Error handling: Use
On Error GoTo ErrorHandlerat the top of every routine. Never use'On Error GoTo ErrorHandler' with proper error loggingin financial scripts—silently skipping errors in payroll data means you could underpay someone by $200 without knowing it.
Assign the macro to a button on the PAYROLL_CALC sheet. When you click it, the populated numbers typically appear in under a second.
Wiring Timesheet Formulas Into Your Payroll Spreadsheet Automatically
Before the macro runs, the timesheet cells need reliable formula links so the VBA reads clean values instead of stale text. In TIMESHEET, use a simple structure: column A = Employee ID, columns B–H = Mon–Sun hours, column I = =SUM(B2:H2), column J = =MAX(0, I2-40) for OT hours.
Then in PAYROLL_CALC, your macro writes =MASTRO.D2 * (TIMESHEET.I2 - TIMESHEET.J2) + MASTRO.D2 * 1.5 * TIMESHEET.J2 into the gross-pay cell for each row. Because every formula follows the same pattern with absolute cell references, the loop simply extends to any new row in MASTRO—adding an employee means updating one table, not editing VBA code.
If you use FILTER on a dynamic range to flag missing timesheet entries, remember it returns #CALC! when no rows match and you omit the [if_empty] argument—always include a fallback value like "No entry" to keep the report readable.
Generating Paystub and Paycheck Reports With One Macro
The second macro takes the calculated gross pay from PAYROLL_CALC and writes a formatted paystub into PAYSTUB_OUT. Each paystub block shows: employee name, pay period dates, hours worked, gross pay, YTD withholding, net pay, and a line-item breakdown of state and federal deductions.
- Formatting: The macro applies a print area per employee block, sets column widths to fit a standard 8.5 × 11-inch page, and inserts page breaks so each paystub prints on its own sheet.
- Batch output: A single loop writes all paystubs sequentially, then triggers
Application.PrintOutif you want hard copies for physical distribution, or simply saves a.pdfviaSaveAsfor email delivery. - Pay period label: Pass the week-start date as a parameter so the macro stamps the correct "Week of" header without you retyping it each Friday.
For a 25-person shop, the full paystub generation typically runs in roughly four seconds on most modern machines.
Fixing the Five Most Common VBA Payroll Spreadsheet Errors
Even a well-built workbook will throw errors in the field. Here are the issues you will hit first and exactly how to resolve them.
| Error / Symptom | Root Cause | Fix |
|---|---|---|
#REF! in gross pay cells | A timesheet row was deleted and the VBA index shifted | Re-anchor the macro to MASTRO IDs, not row numbers |
| Macro runs but writes zeros | Timesheet cells contain text, not numeric hours | Validate with IsNumeric check; add a MsgBox flag for bad entries |
| OT pay not triggered | 40-hour threshold hard-coded instead of pulled from MASTRO | Reference a "Weekly OT Threshold" column so part-time staff at 24 hrs trigger OT correctly |
| Paystub PDF is blank | Print area not set before SaveAs in PDF format | Add Sheet.PageSetup.PrintArea = ... one line before the save call |
#CALC! in FILTER flag column | No matching rows and missing [if_empty] argument | Append , "Pending" as the final argument to FILTER |
Frequently Asked Questions
Can I run this VBA payroll generator in Excel for the Web?
No. VBA macros do not execute in Excel for the Web (browser-based Excel). You need the desktop version of Excel (Microsoft 365 or Excel 2019+) with the Developer tab enabled to run and record these macros. If your whole team works cloud-only, consider migrating the workbook to a local machine or a shared PC with desktop Excel for the Friday payroll run.
How do I handle employees with multiple pay rates or split shifts?
Add a secondary rate column in MASTRO (e.g., "Rate A" for base, "Rate B" for weekend/OT premium). In your VBA loop, check the day-of-week or a flag column in TIMESHEET and apply the correct multiplier before writing to PAYROLL_CALC. Keep the logic in one module so updating a rate means editing one cell, not five formulas across the sheet.
Is it safe to automate payroll if I also file through a service like ADP or Gusto?
Your VBA workbook handles internal time-tracking and reporting. For actual tax filing and fund transfers, keep your existing service as the system of record. Use the Excel payroll report generator as the input layer: export your calculated hours and gross pay, then upload or key those figures into ADP or Gusto for the final run. Always verify totals in both systems before submitting to the IRS or your state Department of Labor. If deductions or withholding logic changes, consult a qualified payroll professional before hard-coding new rules into your macro.