Every Friday afternoon, a small-business office manager opens a 200-cell spreadsheet, copies hours from a handwritten timesheet, pokes overtime formulas into column G, and prays the gross-to-net math holds up. The step-by-step anatomy of a perfect Excel VBA macro workflow for automating weekly payroll and time-tracking reports in US small businesses collapses that ritual into one macro button press: read time data, compute paycheck amounts, generate paystubs, and export a summary report—all inside a structured workbook with a dedicated module. What follows is the exact build sequence you need.

Office manager at desk clicking a macro button in Excel weekly payroll workbook
One button press replaces four hours of manual timesheet entry and paycheck math every Friday.

Setting Up the Workbook Architecture and Module Structure

Before writing a single line of VBA, you need a clean workbook layout. Create a new file called Payroll_Weekly_YYYY.xlsx (update the year each January) with four sheets: Timesheet, Payroll_Calc, Paystub_Output, and Report_Summary. Add a fifth sheet, Config, to hold fixed values like your employer FUTA rate (0.6%) and the current quarterly state withholding percentage.

Open the VBA editor (Alt+F11), insert a standard module named modPayroll, and a second module modTimesheet. Keep public functions in modPayroll and input-validation helpers in modTimesheet. This separation means you can test timesheet parsing without re-running the full payroll chain.

  • Sheet naming convention: Prefix every sheet with its purpose so the macro can reference them by code name: wsTimesheet, wsCalc, wsStub.
  • Config sheet columns: A = parameter name, B = value, C = units or notes. Row 2: FUTA rate; Row 3: state code; Row 4: overtime threshold (40 hours).
  • Macro button: Place a single form control button on Report_Summary that calls Run_Full_Week.
Split screen showing VBA code module beside formatted paystub PDF output
The modPayroll module feeds calculated figures straight into the paystub layout; no manual re-typing.

Building the Time-Tracking Sheet with VBA-Ready Formulas

The Timesheet sheet holds one block per employee. Column A lists employee IDs, Column B shows their name, and Columns C through J track Monday through Sunday hours. Row 2 is the header; Row 3 onward holds data. The critical design choice: store raw clock-in and clock-out timestamps in a hidden sheet (Raw_Punches) and let a VBA function populate the Timesheet sheet automatically.

In modTimesheet, write a function PopulateDailyHours(empID As String, weekStart As Date) that reads Raw_Punches, calculates net hours (subtracting unpaid break time from the Config sheet, typically 0.5 hours), and writes the result into the correct Timesheet cell. Overtime is flagged in a separate column K: =IF(SUM(C3:J3)>40, SUM(C3:J3)-40, 0).

  1. Employee clocks in and out in your timekeeping app (or a simple form sheet) for seven days.
  2. Run PopulateDailyHours from the Report_Summary button. It loops rows 3 through the last active employee row.
  3. Column K auto-calculates overtime hours using the 40-hour threshold stored in Config!B4.
  4. Any cell exceeding 12 hours in a single day triggers a yellow fill via a conditional format rule you set manually—VBA does not need to handle this.

Writing the Payroll Calculation Macro Step by Step

The core macro CalculateWeeklyPayroll lives in modPayroll. It reads each employee's regular hours (total minus overtime) and overtime hours from the Timesheet sheet, pulls the hourly rate from Config column D (one rate per employee row), and computes: Gross = (regular hrs × rate) + (overtime hrs × rate × 1.5).

Next, the macro applies FICA (Social Security 6.2% up to the annual wage base of $176,100 for 2025, Medicare 1.45% with no cap) and the state income-tax withholding rate from Config!B3. Net pay = Gross − all deductions. Each result lands on the Payroll_Calc sheet in a fixed grid so the paystub macro can pull from known cell addresses.

  • Key VBA line: wsCalc.Cells(r, 8).Value = Round(wsTimesheet.Cells(r, 11).Value * wsTimesheet.Cells(r, 7).Value * 1.5, 2) for overtime gross.
  • Guard: Wrap the entire loop in On Error GoTo ErrorHandler. Log the failing employee row to an Error_Log sheet rather than halting the macro.
  • Backup rule: VBA clears the Undo stack the moment a macro runs. Always keep a timestamped copy of the workbook before hitting the button—Ctrl+Z will not recover a macro-triggered overwrite.

Automating the Weekly Paystub and Report Generation

Once Payroll_Calc is populated, the second macro GeneratePaystubs loops each employee row, writes a clean paystub layout onto the Paystub_Output sheet (employee name, pay period dates, gross, each deduction line-item, net pay, year-to-date totals from a lookup table), then exports each row to a PDF named Paystub_EmpID_WeekOf_07072025.pdf in a folder you specify (the macro prompts for a target directory via a folder picker, then constructs the individual PDF file paths).

The final macro ExportReport pulls the Report_Summary sheet—which contains a pivot-table-like summary of total labor cost, total overtime cost, and per-department spend—into a single PDF and emails it to your bookkeeper via Outlook automation (requires the Outlook Object Library reference in the VBA editor).

  1. Run GeneratePaystubs; verify the PDF count matches active employee count.
  2. Run ExportReport; the macro sets Outlook.Application mail, attaches the summary PDF, and sends to the address in Config!B7.
  3. Archive the week's PDFs into a subfolder named with the ISO week number (e.g., 2025-W28).

Error Handling, Validation, and Keeping Numbers Honest

Payroll is not the place for 'On Error GoTo ErrorHandler' with proper error logging. If a macro swallows an exception and writes a zero where a $1,240 net pay should be, your employee gets an incorrect paycheck and you face a correction-notice cycle the next cycle. Instead, every public subroutine in modPayroll carries a labeled ErrorHandler: block that writes the Err.Number, Err.Description, the sheet name, and the failing row to Error_Log, then Exit Sub.

On the Timesheet sheet, add a data-validation list on Column C:J (0–24 allowed hours) so a typo like "55" gets caught before the macro ever sees it. The VBA function ValidateBeforeRun checks that no cell in the active week's block is blank or negative; if validation fails, it highlights the bad cell in red and displays a MsgBox telling you which employee and day need correction. Never let the macro "fix" a number it cannot verify.

  • Never run VBA in Excel for the Web. Macros execute only in desktop Excel (Microsoft 365 or standalone). If your team uses browser-only Excel, host the workbook on a shared network drive and have the office manager run the macro from her desktop instance.
  • Weekly file rotation: Save each week's workbook as Payroll_YYYY-WW.xlsx so you retain a full-year audit trail without a single file growing to 48 tabs.
TaskManual (Hours/Week)VBA-Automated (Minutes)Error Risk Reduction
Enter 30 employee daily hours120 min2 min (macro pulls from punch log)Significantly reduces copy-paste transposition errors
Compute gross, FICA, state tax, net90 min1 min (single loop)Reduces manual rounding and formula-drag mistakes
Generate individual paystub PDFs60 min4 min (batch export)Consistent formatting; fewer missing deduction lines
Compile summary report + email bookkeeper45 min2 min (Outlook automation)Timestamped archive; fewer "forgot to attach" incidents

Scheduling the Full Workflow to Fire Every Friday

The final piece is automation on your calendar. On the Report_Summary sheet, add a macro-bound button that calls Run_Full_Week, which chains all four subroutines in order: validate → populate timesheet → calculate payroll → generate stubs and report. Click it once, wait roughly 45 seconds for 30 employees, and the week is done.

For teams that want hands-off execution, create a Windows Task Scheduler entry (or a .bat file that launches Excel via command line: excel "c:\work\payroll\Payroll_2025-W28.xlsx" /m "Run_Full_Week") set to fire at 6:00 AM Friday. The command opens the current week's workbook and executes Run_Full_Week directly, then saves a completion timestamp to Config!C9. If the file is locked because someone still has it open, the task logs a failure email so you know before payroll night.

  • Pre-Friday checklist (manual, 10 min): Confirm all punch data is uploaded, verify no employee's hours exceed 12/day on the Timesheet sheet, and check that Config rates match your current pay table.
  • Post-run verification: Open the PDF report, spot-check two employees (one with overtime, one without), and compare net pay against your manual backup calculation for the first two weeks of go-live.
Disclaimer: This content is provided for general informational purposes only and does not constitute official financial, tax, or legal advice. Regulations vary by jurisdiction; consult a certified financial planner, accountant, or tax professional for your specific circumstances.

Frequently Asked Questions

Can I use this VBA payroll workflow if I run Microsoft 365 in the browser?

No. VBA macros execute only in the desktop version of Excel (Windows or Mac). If your team lives in Excel for the Web, designate one person—the office manager—to run the macro from a desktop installation on a shared-drive copy of the workbook. The browser version will not run the Run_Full_Week subroutine.

What happens to the Undo stack when my macro writes to 300 cells at once?

As soon as VBA begins writing, the entire Undo history for that workbook session is cleared. Ctrl+Z will not revert the macro's changes. Always save a timestamped backup copy of the workbook (Payroll_YYYY-WW_backup.xlsx) before pressing the run button. This is essential for any financial or payroll data script.

How do I handle an employee who is new mid-week and has fewer than seven days of punch data?

In the ValidateBeforeRun function, check the employee's start date stored in Config column E. If the start date falls within the current week, the macro treats any day before that date as zero hours (not an error) and calculates regular and overtime pay only for the days the employee was actually scheduled. The paystub PDF will show the correct proration and note the short pay period.