Use Power Query when your job is to import, clean and combine data that changes regularly, and use VBA when you need Excel to do something the user interface cannot do, such as drive events, call other programs or build interactive forms. In practice most good workbooks use both: Power Query shapes the data, VBA or a macro button runs the routine.

Understanding where each tool stops being the right choice matters more than knowing either in detail, because the wrong pick leads to slow, fragile workbooks that break whenever the source file changes shape.

What You'll Need

  • Excel for Microsoft 365, Excel 2021, 2019 or 2016 on Windows, or Excel for Mac with a reasonably current version. Menu labels and Power Query availability differ slightly by version, device and region.
  • Power Query enabled. On modern Excel it is built in under the Data tab, in the Get & Transform Data group. On older builds the Power Query tab appears after you enable the add-in.
  • Macro access if you intend to use VBA: File > Options > Customize Ribbon with Developer ticked. On Mac this is under Excel > Preferences.
  • A workbook saved as .xlsm if it will contain macros. .xlsx cannot store VBA.
  • Macro security set to the default Disable all macros with notification rather than Enable all.

Step-by-Step

  1. Describe the job in one sentence. "Pull three monthly CSV files, drop blank rows, split the customer name, and pivot the total by region" is a data-shaping job. "When someone edits a cell, stamp the date and time and highlight the row" is an Excel-behaviour job. Say it out loud before opening anything.
  2. Check whether the answer recurs on a schedule. If the source data is refreshed weekly or monthly and the structure is stable, Power Query wins because the refresh repeats with one click. If the job is a one-off cleanup, either tool works and Power Query is usually faster to build.
  3. Test Power Query first for any import or cleanup. Go to Data > Get Data, choose the source (File, Folder, Workbook, CSV and so on), then open Transform Data. Every action you take is recorded as a step in the Applied Steps list, and you can rename, reorder or delete any step later.
  4. Verify the query logic survives a row shift. Power Query refers to columns by name, not position, so inserting a column in the source will not silently break the result the way a recorded VBA macro can.
  5. Load the result to a table or to the Data Model. Choose Close & Load > Close & Load To and pick a worksheet table, a pivot table or a connection only. A connection only is best for large sources because nothing is written into a sheet.
  6. Write down where the query lives. In Data > Queries & Connections you can see every query, and the Power Query Editor shows the underlying M language in the formula bar or in the advanced editor. That text is the real query, so keep a copy if the file is shared.
  7. Reach for VBA only for actions the UI cannot do. That means opening and saving other files, shelling out to other programs, showing user forms, controlling Excel events through the Workbook or Worksheet code modules, or repeatedly applying formatting that Power Query cannot express.
  8. Record first, then clean the code. With the Developer tab open, use Record Macro, perform the action once, then Stop Recording and open Visual Basic to read what was written. Recorder output is verbose and contains hard-coded ranges and selections, which you replace with explicit sheet and range references.
  9. Replace hard-coded ranges with variables. Instead of Range("A1:D40"), define the last row with a variable or use a named table reference, so the macro keeps working when rows are added. This is the single most important change when moving recorded code into production.
  10. Add error handling. Wrap the main body in On Error GoTo with a label that reports what failed, and switch off ScreenUpdating and Calculation at the start, restoring both before exit. Without this a runtime error leaves Excel frozen in a half-finished state.
  11. Join the two together. A common pattern is a Power Query query refreshing on open, then a short macro that triggers ThisWorkbook.RefreshAll and waits for the refresh to finish before continuing. Keep the macro short and let Power Query own the transformation logic.
  12. Document the workbook before handing it over. Name your queries and macros descriptively, put a short note on a hidden or first sheet listing what each does and when it should be run, and record whether the source paths are absolute or relative.

Common Mistakes

  • Recording a macro to do data cleaning. Recorder code depends on exact sheet names, exact cell positions and the current selection. When the layout shifts by one column, the macro either fails or quietly puts the wrong value in the wrong column.
  • Believing Power Query can read your mind about headers. Promoted or demoted headers are a recorded step. If the source gains a new title row, the step must be edited, and a broken header step often shows as Column1 instead of the real name.
  • Building one giant query. Long applied-step lists are hard to audit. Split source import, cleaning and final output into separate queries and reference them, so you can inspect each stage.
  • Hard-coding file paths in queries. Pointing a query at one person's Downloads folder makes the workbook unusable for anyone else. Use a parameter or a cell-driven path instead.
  • Using VBA where Power Query is enough. Loops that walk row by row through tens of thousands of cells are slow and crash-prone. Power Query processes the whole table in one vectorised operation.
  • Saving a macro workbook as .xlsx. Excel strips the macros with only a warning dialog, and the code is gone when you reopen the file.
  • Leaving macro security set to Enable all. That removes the main safeguard against an unexpected file running code on your machine.

Tips

  • Choose Power Query when the deliverable is data, and VBA when the deliverable is behaviour. That single rule resolves most debates.
  • Keep the applied steps short and renamed where useful. A reader should be able to follow the chain from source to output without opening the advanced editor.
  • Use tables, not plain ranges, as Power Query sources. Tables expand with new data and give the query a stable name to bind to.
  • Store macro code in modules, not in sheet code modules, unless the code genuinely needs to respond to an event.
  • Version workbooks with a date or revision number in the file name. Both queries and macros are hard to diff, and an older copy is often the fastest fix.
  • For shared files, keep the Power Query logic and the macro in one workbook so the two cannot drift out of sync.
  • If a refresh should not run on every open, set it to refresh manually and provide a clearly labelled button instead.

Troubleshooting

When a query will not refresh, reopen the editor and look at the failing step rather than at the output. The step itself is usually highlighted, and the message names the action that broke, such as a missing column or an unexpected value type. Renaming the step helps you find it again. If the source file moved, update the path in the query settings rather than rebuilding the query. If a whole column comes back empty, check whether the type was set to text before the cleaning step, because a type change applied too early prevents later splits and replacements from working correctly.

When a macro does nothing, first press the Stop button in the editor in case it is still paused, then run the procedure directly from the Visual Basic editor to see the real error message. Check that macros are enabled for the file location and that the workbook is not open in a protected view. A macro that worked once and now fails usually points at a renamed sheet, a moved range or a missing add-in rather than at the code itself.

If Excel has become slow, look for query refreshes firing on open, volatile formulas and per-row macro loops. Disable automatic refresh temporarily to confirm which one is responsible, then restructure the slow part rather than adding more code around it.

If a workbook is shared and behaves differently for a colleague, compare Excel versions and operating systems first. Power Query connectors and menu wording vary between versions, and some connectors need the correct credentials stored on that machine.

Frequently Asked Questions

Can Power Query replace VBA entirely?

No. Power Query handles import, cleaning and reshaping, but it cannot respond to cell edits, show interactive forms or control other applications. VBA handles those behaviour tasks.

Does Power Query work on Mac?

Power Query is available in modern Excel for Mac, but not every connector and feature exists in the Mac version. Check the source type you need before relying on it.

Is Power Query faster than VBA?

For bulk data transformation, yes, because it works on the whole table rather than looping row by row. For small one-off formatting jobs the difference is negligible.

Do I need to learn M code or VBA to start?

No. Power Query is largely point-and-click and records your actions as steps, and VBA can be recorded, so you can start with the interface and learn the underlying text only when the recorded version falls short.

Can a workbook use both at once?

Yes, and that is often the best design. Let Power Query own the data pipeline and keep the macro limited to refreshing, navigation and output.