VBA vs Power Query: Which Should You Learn for Excel?
By ExcelReady TeamUpdated 6 min read
The short answer
Learn Power Query first if your work is importing, cleaning and combining data: you click through steps, it needs no code, and one refresh repeats the work. Learn VBA when you need to automate things Power Query can't do, such as formatting reports, creating sheets, or running actions when a button is clicked. Many analysts end up using both.
Key takeaways
- Power Query is for getting and shaping data. VBA is a programming language that can automate almost any action in Excel.
- Power Query needs no code for most jobs and saves in a normal .xlsx file. VBA needs a macro-enabled file such as .xlsm.
- VBA doesn't run in Excel for the web, and macro actions can't be undone with Ctrl+Z.
- Power Query's Trim removes only leading and trailing spaces. Excel's TRIM function also shrinks double spaces inside text.
- For most job seekers, Power Query is the faster skill to learn and the easier one to share.
On this page+
VBA vs Power Query comes down to what you want to automate. Power Query is built for getting and cleaning data: you click through steps once, and a refresh repeats them. VBA is a programming language that can automate almost anything you can do in Excel, from formatting reports to creating new files. This guide compares them side by side and helps you decide which one to learn first for work or a job search.
VBA vs Power Query at a glance#
Power Query is the easier, safer tool for data. VBA is the more powerful tool for everything else.
| Feature | VBA | Power Query |
|---|---|---|
| What it is | A programming language inside Excel | A data import and transform tool |
| Best for | Automating actions: formatting, sheets, files, buttons | Importing, cleaning, reshaping and combining data |
| How you build it | Write code, or record a macro and edit it | Click steps in the Power Query Editor (code optional) |
| Repeating the work | Run the macro | Refresh |
| File type | Macro-enabled, such as .xlsm | Normal .xlsx works |
| Undo | Macro actions can't be undone with Ctrl+Z | Edit or delete any step |
| Excel for the web | Doesn't run | Can refresh some queries; building them is limited |
| Mac | Runs, with some Windows-only features missing | In recent Microsoft 365 versions |
| Learning curve | Steeper: variables, loops, objects | Gentler for common tasks |
How each one works#
Power Query lives on the Data tab under Get & Transform Data. You connect to a source, such as a table, a CSV file or a folder of files. Then you apply steps like "remove blank rows", "trim text" or "split column". Each step is listed under Applied Steps, and the result loads back into Excel as a table. Behind the scenes, the steps are written in a language called M, but you rarely need to touch it. The guide to Power Query for beginners walks through a first query.
VBA (Visual Basic for Applications) is code stored in the workbook. You write it in the Visual Basic Editor (Alt+F11 on Windows; on a Mac, Tools > Macro > Visual Basic Editor), or you record your actions with the macro recorder and let Excel write the code. A macro can touch any part of Excel: cells, formats, sheets, charts, files and other Office apps. See Excel macros for beginners to record a first one.
The same task done both ways#
Here is a common job: each month you get a customer list with messy names, like " north star foods ", and you need them clean.
With Power Query#
- Click inside the data and choose Data > From Table/Range.
- Select the Customer column.
- Choose Transform > Format > Trim, then Transform > Format > Capitalize Each Word.
- Choose Home > Close & Load.
Next month, paste the new list into the source table and choose Data > Refresh All. One thing to know: Power Query's Trim removes spaces at the start and end only. It does not shrink double spaces inside the text, so "north star" keeps its extra spaces. You need an extra step, such as Replace Values, for those.
With VBA#
Sub CleanNames()
Dim c As Range
For Each c In Range("A2:A100")
c.Value = Application.WorksheetFunction.Proper( _
Application.WorksheetFunction.Trim(c.Value))
Next c
End SubThis loops through A2:A100 and overwrites each name with a clean version. It uses Excel's own TRIM, which removes extra spaces inside the text too. VBA's built-in Trim function only removes spaces at the ends, which is a common surprise. The macro also overwrites the original data, and Ctrl+Z can't bring it back, so test on a copy.
With a formula#
For a one-off job, the formula in the exercise does the same thing: =PROPER(TRIM(A2)) turns " north star foods " into North Star Foods. The TRIM function guide explains exactly which spaces it removes, and Power Query vs formulas compares those two approaches.
When to use Power Query#
Power Query is the better choice when the job is about data coming in:
- Combining files or sheets. Append a folder of monthly CSV files, or stack sheets with the same columns. The guide to combining data from multiple sheets shows this.
- Cleaning repeated exports. The same clean-up every week becomes one refresh.
- Reshaping data. Unpivot a wide report into a tidy table, or split and merge columns.
- Merging tables. Join two lists on a shared ID, much like a lookup.
- Sharing files. The workbook stays a normal .xlsx, so colleagues don't see macro warnings.
When to use VBA#
VBA is the better choice when you need actions, not just data:
- Formatting and layout. Apply styles, set print areas, hide sheets or build a report layout.
- Creating files. Save each region's report as its own workbook or PDF.
- Buttons and events. Run code when someone clicks a button, changes a cell or opens the file.
- Working across apps. Create Outlook emails or update a Word document from Excel data.
- Loops with logic. Step through rows and decide what to do with each one.
Because VBA needs a macro-enabled file, read .xlsx vs .xlsm before you share one. Many organisations block macros in files that come from the internet or email, so a VBA solution may not run on someone else's computer without extra steps.
Common mistakes#
These cause the most trouble with each tool:
- Using VBA to clean data that Power Query could handle. The code is harder to maintain, and only people who read VBA can fix it.
- Running a macro on the only copy. VBA changes can't be undone. Save first or work on a copy.
- Expecting Power Query to update by itself. It updates when you refresh, not when the source changes.
- Changing column names in the source. Both a query step and a macro that look for "Customer" break when it becomes "Client".
- Forgetting where the file will be used. VBA won't run in Excel for the web, so a browser-only team can't use it.
- Hard-coding sheet names. A macro that points at
Sheets("Jan")fails when someone renames the tab. The cross-sheet references lesson shows how formulas handle sheet names instead.
Our recommendation#
Learn Power Query first. It covers the most common automation need, cleaning and combining data, without code, and the files it makes are easy to share. Most people can build a useful query in an afternoon.
Add VBA when you hit a wall. If you need to format reports, create files, react to buttons or work with other Office apps, VBA is still the tool for the job in desktop Excel. For work in Excel for the web, look at Office Scripts instead.
Use them together when it helps. They are not rivals. A common setup is a Power Query that cleans and combines the data, plus a short macro that refreshes it, formats the report and saves a PDF for each region. Power Query does the data work it is good at, and VBA handles the actions around it. Keeping the data steps in Power Query also means the macro stays short and easier for someone else to read.
For job seekers: strong formulas and Power Query are asked for more often than VBA in many analyst and operations roles, but check the job ads in your own field.
Practise it#
Both tools automate clean-up steps that you should first understand by hand. Try the exercise on this page, then practise cleaning messy text in a live spreadsheet with TRIM, PROPER and SUBSTITUTE, and use the worksheets and cross-sheet references lesson to work across tabs. Each lesson checks your formulas instantly.
Try it yourself
This is a live spreadsheet. Type the formula in the green cell and press Enter.
This is the kind of clean-up both tools automate. In C2, clean the name in A2 so the extra spaces are removed and each word starts with a capital. Use =PROPER(TRIM(A2)) and press Enter.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Customer (clean) | |||
| 2 | North | |||
| 3 | South | |||
| 4 | East | |||
| 5 | North | |||
| 6 | ||||
| 7 | ||||
| 8 |
Tip: TRIM removes spaces at the start and end and shrinks double spaces to one. PROPER then capitalises each word.
Practise this in a lesson
Frequently asked questions
Is Power Query replacing VBA?
Not fully. Power Query has taken over many data import and cleaning jobs that people used to write VBA for, but VBA can still do things Power Query can't, such as formatting sheets, creating files and reacting to button clicks.
Should I learn VBA or Power Query first?
For most people, Power Query first. It covers the common job of cleaning and combining data, needs no code to start, and works in a normal workbook. Learn VBA later if you need to automate actions beyond data shaping.
Can Power Query do everything VBA can?
No. Power Query only gets data, transforms it and loads the result as a table or connection. It can't format cells, create charts, send emails or respond to clicks, which VBA can.
Does Power Query need macros enabled?
No. Power Query is not a macro, so the workbook can stay a normal .xlsx file. You may still see a security prompt when a query connects to an outside data source.
Is VBA still worth learning?
It can be, especially in roles with lots of desktop Excel automation, such as finance and operations reporting. Check job ads in your field, because many roles now ask for Power Query or formulas more than VBA.