Expert guide

Power Query vs VBA: Which Should You Use in Excel?

Power Query for data, VBA for actions, and both together for most real workbooks

Short answer: use Power Query to get, clean, reshape and combine data, because it is repeatable, macro-free and refreshes in one click. Use VBA to perform actions: format reports, save and email files, print, react to buttons and events, or drive Outlook and Word. Most robust workbooks use Power Query for the data pipeline and a little VBA (or Office Scripts) for output. We design that split in our Excel automation projects.

Ten years ago, almost every Excel automation was a macro. Today many of those macros are thousands of lines of VBA that copy, paste, delete blank rows and fix data types, all things Power Query does with a few recorded steps. The question is no longer "which is better" but "which part of this job belongs to which tool".

Task-by-Task Comparison

TaskPower QueryVBA
Import CSV, Excel, database, web dataExcellent, many connectorsPossible, more code
Combine all files in a folderExcellent (From Folder)Good with a loop
Clean: trim, split, types, remove rowsExcellent, recorded stepsTedious to write and maintain
Unpivot, group, merge (join)ExcellentHard
Format sheets, colours, layoutNoYes
Save as PDF, save copies, emailNoYes
Respond to buttons and eventsNoYes
Drive Outlook, Word, other appsNoYes (desktop)
Works where macros are blockedYes (.xlsx)No
Refresh on a schedule in the cloudVia Power BI or Power Automate scenariosNot natively

Why Power Query Wins for Data Preparation

  • Repeatable

    Every step is recorded; next month you click Refresh.

  • Transparent

    Applied Steps show exactly what happens to the data, in order.

  • Robust to size

    Handles hundreds of thousands of rows without loops.

  • No macro security

    Works in .xlsx files that IT policies allow everywhere.

  • Non-destructive

    The source stays untouched; the output table is rebuilt each refresh.

Why VBA Still Matters

  • Producing outputs: formatted reports, one file per branch, PDFs.

  • Distribution: emailing workbooks or PDFs through Outlook.

  • User interaction: forms, buttons, validation on entry, prompts.

  • Working with other Office applications on the desktop.

  • Legacy processes that would be costly to rebuild and work fine.

The Hybrid Pattern

1. Power Query: import + clean + combine  →  tblClean (refreshable)
2. Formulas / PivotTables: calculations and summaries on tblClean
3. VBA (one short macro):
      ThisWorkbook.RefreshAll
      Application.CalculateUntilAsyncQueriesDone
      ' format, export PDFs, email
4. Optional: Power Automate / Office Scripts for scheduled cloud runs

The macro shrinks from hundreds of lines to a few dozen, and the fragile data-wrangling code disappears. When the source layout changes, you adjust a Power Query step instead of debugging a loop.

Sub RefreshAndPublish()
    Application.ScreenUpdating = False
    ThisWorkbook.RefreshAll
    Application.CalculateUntilAsyncQueriesDone
    ThisWorkbook.Worksheets("Report").ExportAsFixedFormat xlTypePDF, _
        ThisWorkbook.Path & "\Weekly Report " & Format(Date, "yyyy-mm-dd") & ".pdf"
    Application.ScreenUpdating = True
End Sub

Migrating an Old Macro to Power Query

  • Read the macro and list what it does to data (import, filter, delete rows, split columns, lookups).

  • Rebuild those steps in Power Query from the same source files.

  • Compare output row counts and totals with the macro's output for a few periods.

  • Keep only the output steps (formatting, saving, emailing) in VBA.

  • Document the query steps' purpose with clear step names.

AI assistants can explain the old macro and draft the M code; see writing Power Query M with AI and using AI with Excel VBA.

Where Office Scripts Fit

Office Scripts are a modern option for workbooks stored in OneDrive or SharePoint, especially when automation should run on a schedule through Power Automate without anyone opening Excel. They complement Power Query in the cloud the way VBA complements it on the desktop. A full comparison is in Office Scripts vs VBA.

Decision Guide

  • Is the task mostly about data coming in? Power Query.

  • Is the task mostly about doing something with the result? VBA (desktop) or Office Scripts (cloud).

  • Are macros blocked for some users? Push as much as possible into Power Query.

  • Must it run unattended on a schedule? Consider Power Automate with Office Scripts, or a server-side process.

  • Is it a one-off? Do whatever is fastest, then automate only if it repeats.

For many files in a folder, both approaches are shown in looping through files with VBA and Excel data consolidation.

FAQ

Power Query vs VBA FAQ

Choosing the right Excel automation tool

For getting, cleaning, reshaping and combining data, usually yes: it is repeatable, needs no macros, records each step, and refreshes with one click. For automating actions, such as formatting output, saving and emailing files, interacting with Outlook or Word, or responding to user clicks, VBA is the right tool. Many good solutions use both.

Not completely. Power Query cannot save files, send email, print, change formatting on sheets, or react to events. It replaces the data preparation part of many macros, which is often the largest and most fragile part.

Yes. Power Query works in normal .xlsx files, so it is not affected by macro security settings that block .xlsm files. That is a big advantage in organisations that restrict macros.

For large data transformations, Power Query is typically faster and simpler than equivalent VBA, especially compared with cell-by-cell loops. Well-written VBA using arrays can also be fast. Performance rarely decides the choice; maintainability and the type of task usually do.

Office Scripts are TypeScript automations for Excel on the web and Microsoft 365, and can run from Power Automate. They suit cloud-stored workbooks and scheduled flows, but cannot automate desktop applications such as classic Outlook or Word the way VBA can.