Excel VBA automates Power Query by letting you write a macro that fires query refresh, cleans raw data, and posts entries to your ledger in one click. If you spend your Tuesday evenings matching a client's Stripe export against their bank feed, this workflow can cut that to minutes. Below is the full beginner setup, from enabling the Developer tab to your first reconciliation subroutine.
What VBA Automation Actually Does for Your Bookkeeping Practice
VBA (Visual Basic for Applications) is the scripting layer built into desktop Excel. You store code in a module, name your subroutines and functions, and run them against any worksheet in the workbook. For freelance bookkeeping, that means a macro can open a client's spreadsheet, pull a bank CSV through Power Query, flag unmatched invoices, and write results back to the general ledger—hands off.
Think of Power Query as the ingestion engine and VBA as the trigger-and-control layer. Power Query handles the messy part: changing column headers, splitting transaction notes, removing duplicate rows. VBA handles the sequence: refresh the query, then run a VLOOKUP-based match, then populate the reconciliation sheet. Together they can replace the copy-paste-and-filter loop that eats your billable hours.
Small-business owners you serve typically do not understand this stack. You position yourself as the person who builds their spreadsheet once, then maintains a template they can hand to a new bookkeeper with minimal retraining.
- What VBA adds over plain formulas: sequencing, conditional branching across multiple worksheets, and the ability to call Power Query refresh programmatically.
- What Power Query adds over VBA alone: reproducible data-connection pipelines that survive file updates without rewriting script logic.
Connecting Power Query and VBA Inside a Single Workbook
Before you write a single line of code, structure the workbook correctly. Create one sheet for raw data (where Power Query drops transformed output), one for the general ledger, one for invoices, and one for payroll. Keep the VBA project in a single module per client so you can export, version, and hand off cleanly.
- Enable the Developer tab: File > Options > Customize Ribbon > check Developer.
- Import your client's bank export via Data > Get & Transform (Power Query). Map columns, remove unwanted fields, and load the query to a worksheet called
RawBank. - Open the VBE (Alt+F11). Insert a new module. Type this refresh subroutine:
Sub RefreshAllData() : ThisWorkbook.RefreshAll : End Sub. - Assign that macro to a button on your control sheet so your client can hit one key each month.
Because VBA cannot execute inside Excel for the Web, always confirm your client opens the .xlsm file on desktop. If they use only the browser version, you will need to split the workflow: they refresh queries in the web app, and you run the VBA macros locally during your review pass.
Writing Your First Reconciliation Macro for Invoice Matching
Your first real macro should solve the most painful task: matching a client's invoices against their bank deposits. Power Query has already loaded clean transaction rows into RawBank. Your VBA subroutine now loops through each open invoice on the Invoices sheet, looks up the amount in RawBank, and writes a matched flag.
Use On Error GoTo ErrorHandler at the top of the sub, not 'On Error GoTo ErrorHandler' with proper error logging. In accounting work, silently swallowing a type-mismatch or a missing cell can post a $0 line to the ledger and create a discrepancy you will not find until year-end. A proper error handler logs the row number and the offending value into a hidden debug sheet so you can trace the break.
- Subroutine skeleton: a
For Eachloop over invoice rows, anApplication.WorksheetFunction.Matchagainst the amount column, and a.Cells(row, flagCol).Value = "Matched"write. - Guard rail: wrap the loop in
Application.Calculation = xlCalculationManualso the worksheet does not recalculate every iteration, then restore toxlCalculationAutomaticwhen the loop ends.
Automating Ledger and Payroll Routines Without Breaking Formulas
Once reconciliation is clean, the next bottleneck is posting. A payroll run for a 12-person shop means updating the payroll register, writing journal entries to the ledger, and recalculating tax-withholding formulas. A single VBA subroutine can open the payroll worksheet, read the net-pay column, and write the corresponding debit/credit rows to the ledger sheet without touching your existing SUM or VLOOKUP formulas on other tabs.
The critical rule: never let a macro overwrite a cell that contains a formula. Read the formula first with Range.Formula; if it is not a simple number, skip that row and log a warning. This keeps your template intact for the next month and prevents the "my spreadsheet no longer calculates" phone call from the client.
| Task | Manual Time (12-employee client) | VBA + Power Query Time | Where the Script Lives |
|---|---|---|---|
| Bank-feed ingestion & column mapping | 35 min | 2 min (auto-refresh) | Power Query, loaded to RawBank |
| Invoice-to-deposit matching | 40 min | 1 min (MatchSub) | Module1, Sub ReconcileInvoices |
| Payroll journal posting | 55 min | 3 min (PostSub) | Module1, Sub PostPayrollEntries |
| Monthly close report | 30 min | Auto (single button) | CommandButton on Control sheet |
Notice the time column: a monthly close that took two hours and forty minutes can finish in under nine. That gap represents billable savings you can pass to the client or bank as margin.
Troubleshooting the Beginner Mistakes That Stall Your Macros
Three errors account for many first-week frustrations. First, a macro clears the Undo stack the moment it runs, so Ctrl+Z will not roll back a batch of posted ledger rows. Always save a timestamped copy before you hit Run. Second, ThisWorkbook.RefreshAll refreshes every query in the file; if a second client's query is accidentally in the same workbook, you overwrite their data. Scope refreshes to a specific QueryTable object when a file serves multiple clients.
Third, if your client sends you the file from a phone or iPad, the VBA module is invisible—Excel for the Web strips it entirely. Keep a canonical .xlsm on your desktop or a shared drive, and tell the client to export a copy rather than editing the master.
- Debugging tip: set
Option Explicitat the top of every module. The compiler will flag an untyped variable that is silently returningEmptyin a financial calculation. - Logging habit: write a timestamp and the client's name into a
RunLogsheet at the start of each subroutine. When a discrepancy appears mid-year, you have an audit trail instead of guessing which Tuesday the file changed.
Frequently Asked Questions
Can I run VBA macros that trigger Power Query in Excel Online or on my phone?
No. VBA subroutines and ThisWorkbook.RefreshAll only execute in desktop Excel (.xlsm). In Excel for the Web or the mobile app, you can refresh a Power Query connection manually, but the scripting layer is stripped. Keep your master macro file on a desktop machine and share a sanitized .xlsx version with clients who need to view data remotely.
How many VBA modules do I realistically need for a single-client bookkeeping template?
Most freelance bookkeeping templates run on two modules. Module 1 handles data ingestion (query refresh, column mapping, deduplication). Module 2 handles posting (reconciliation flags, ledger journal entries, payroll write-backs). A third small module for reporting and PDF export keeps things tidy as the template grows. Export the whole VBA project as a .bas file each time you change code so you can roll back a broken update in one step.
Will automating ledger posting with macros create a compliance or audit problem for my clients?
In most cases, not—if you log every automated run. Build a RunLog sheet that timestamps each subroutine execution, records the client name, and saves the source row count before and after posting. Your client's CPA or external auditor can trace any ledger line back to a specific macro run. The macro itself is the tool; the log is the paper trail that keeps it defensible.