VBA course · Lesson 1 of 33
What is VBA? Macros, when to use them and macro security
Learn what VBA and macros are, when VBA beats formulas or Power Query, how .xlsm files work and how to set macro security safely in the Trust Center.
VBA stands for Visual Basic for Applications. It is the programming language built into Excel (and Word, Outlook, PowerPoint and Access). With VBA you write short programs, called macros, that tell Excel what to do step by step: open a file, copy a range, format a report, send an email, loop through a thousand rows and fix each one. Anything you can do with the mouse and keyboard, a macro can usually do faster, and it does it the same way every time.
This course starts from zero. You do not need any programming experience, only a basic feeling for how Excel works: what a cell, a range, a worksheet and a workbook are. If formulas such as SUM or VLOOKUP are new to you, it is worth looking at the Excel course first, because VBA builds on the same ideas.
What is a macro?
A macro is a named set of instructions stored inside a workbook. When you run it, Excel carries out the instructions one after another. Here is a complete macro. Do not worry about the details yet; you will write this yourself in Your first macro: Sub, MsgBox, running it and a button.
Sub WriteHeading()
' Put a title in cell A1 of the active sheet
Range("A1").Value = "Monthly sales report"
Range("A1").Font.Bold = True
MsgBox "The heading is ready."
End SubEvery macro starts with Sub followed by its name and a pair of brackets, and ends with End Sub. The lines in between are the instructions. A line that starts with an apostrophe (') is a comment: Excel ignores it, but people reading the code later will thank you for it.
When you run WriteHeading, three things happen in less than a blink: A1 gets the text, A1 becomes bold, and a small message box pops up. Now imagine that the instructions were "for each of the 40 branch files in this folder, open it, copy the totals into the summary sheet, close it". That is where macros start to save hours.
Here is a slightly bigger taste of what is coming. Imagine an invoice list with due dates in column C and a status in column D. This macro walks down the list and marks every unpaid invoice whose due date has passed. You will understand every line of it by the end of For loops: For...Next and For Each.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Invoice | Customer | Due date | Status |
| 2 | INV-0141 | Contoso Ltd | 2026-11-15 | Paid |
| 3 | INV-0142 | Fabrikam | 2026-11-20 | Open |
| 4 | INV-0143 | Tailspin Toys | 2026-12-10 | Open |
| 5 | INV-0144 | Adventure Works | 2026-11-28 | Open |
Invoice list before the macro runs (today is 2026-12-02)
Option Explicit
Sub MarkOverdueInvoices()
Dim r As Long
Dim lastRow As Long
' Last filled row in column A
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
For r = 2 To lastRow
If Cells(r, "D").Value = "Open" And Cells(r, "C").Value < Date Then
Cells(r, "D").Value = "Overdue"
Cells(r, "D").Font.Color = RGB(192, 0, 0)
End If
Next r
End Sub| A | B | C | D | |
|---|---|---|---|---|
| 1 | Invoice | Customer | Due date | Status |
| 2 | INV-0141 | Contoso Ltd | 2026-11-15 | Paid |
| 3 | INV-0142 | Fabrikam | 2026-11-20 | Overdue |
| 4 | INV-0143 | Tailspin Toys | 2026-12-10 | Open |
| 5 | INV-0144 | Adventure Works | 2026-11-28 | Overdue |
After the macro: two open invoices are past their due date
You could get a similar display with conditional formatting, but the macro actually changes the status text, which can then feed a reminder email or a report. That difference, between showing and doing, is the heart of when to use VBA.
The third example is a one-liner habit you will use in every lesson. Debug.Print writes into the editor's Immediate window, which is a quick way to look at values while you learn:
Option Explicit
Sub ShowWorkbookInfo()
Debug.Print "Workbook: " & ThisWorkbook.Name
Debug.Print "Sheets: " & ThisWorkbook.Worksheets.Count
Debug.Print "Excel version: " & Application.Version
End SubMacro, VBA, code: are they the same thing?
People use the words loosely. Strictly speaking, VBA is the language, code is the text you write in that language, and a macro is one runnable procedure (one Sub). A workbook can contain dozens of macros. In everyday talk "a macro" often just means "some VBA that automates something", and that is fine.
What can VBA do that formulas cannot?
Formulas are brilliant at calculating: a cell looks at other cells and shows a result. But a formula can only change the cell it lives in. It cannot insert rows, rename sheets, save a file or send an email. VBA can act. Typical jobs for VBA are:
- Repeating the same formatting and clean-up steps on every new data export.
- Combining many workbooks or sheets into one summary.
- Creating one PDF invoice per customer and saving each with the right file name.
- Building custom data-entry forms (UserForms: build a data-entry form in VBA) so people cannot type into the wrong cell.
- Reacting automatically when something changes, for example adding a timestamp when a status is set to "Done" (Events: macros that run by themselves).
- Writing your own worksheet functions when no built-in function does what you need (User-defined functions (UDFs): your own worksheet functions).
- Talking to other programs: Outlook, Word, text files, folders on disk (Control Outlook, Word and PowerPoint from Excel VBA).
When not to use VBA
A good Excel developer reaches for VBA last, not first. Before you write a macro, ask whether one of these tools does the job with less risk:
| Task | Often better tool | Why |
|---|---|---|
| Calculate totals, lookups, ratios | Formulas such as SUMIFS, XLOOKUP | They update by themselves and anyone can read them |
| Filter or sort a list for a report | FILTER, SORT, tables | Dynamic arrays recalculate without a button |
| Import and clean the same CSV every month | Power Query | Steps are recorded, repeatable and need no macro security |
| Summarise large data by category | PivotTables | Built in, fast and interactive |
| Highlight late invoices | Conditional formatting | No code to maintain |
VBA shines when the task involves actions (create, move, save, send, delete), many files, user interaction, or logic too awkward for formulas. A good rule of thumb: if you can describe the job as "every time I get this file, I do these 15 clicks", it is a candidate for a macro. If you can describe it as "this cell should always show...", it is a job for a formula.
Where VBA lives: the .xlsm file
A normal workbook saved as .xlsx cannot contain macros. If you write VBA in a workbook and then save it as .xlsx, Excel warns you that the VB project will be removed, and your code is gone when you close the file. To keep macros you must save as one of these types:
| Extension | What it is | Contains macros? |
|---|---|---|
| .xlsx | Standard workbook | No |
| .xlsm | Macro-enabled workbook | Yes |
| .xlsb | Binary workbook (smaller, opens faster) | Yes |
| .xltm | Macro-enabled template | Yes |
| .xlam | Add-in (code that is loaded into every workbook) | Yes |
To save as .xlsm: choose File > Save As, pick a folder, and in the Save as type list (Mac: File Format) choose Excel Macro-Enabled Workbook (*.xlsm).
.xlsm instead.There is also a special hidden workbook called the Personal Macro Workbook (PERSONAL.XLSB). Macros stored there are available in every workbook you open on your computer. You will meet it in The macro recorder: record, read and clean up code and VBA best practices: clean code, sharing and security.
Macro security and the Trust Center
Because a macro can delete files or send emails, it can also be used for harm. Malicious macros hidden in email attachments were for years one of the most common ways to spread viruses. Excel therefore does not run macros from unknown files by default.
When you open a macro-enabled file, one of these usually happens:
- A yellow Security Warning bar appears: "Macros have been disabled." with an Enable Content button. Clicking it trusts this file and runs its macros.
- A red bar says Security Risk: Microsoft has blocked macros from running because the source of this file is untrusted. This appears for files downloaded from the internet or received by email (they carry a "Mark of the Web"). There is no enable button here on purpose.
- Nothing appears, because the file is in a trusted location or signed by a trusted publisher.
Unblocking a file you trust
If you downloaded a workbook from a source you really trust (for example the sample files of this course or your own company's SharePoint), close it, right-click the file in File Explorer, choose Properties, tick Unblock at the bottom of the General tab and click OK. When you open it again you get the normal yellow bar.
The macro settings
The settings live in File > Options > Trust Center > Trust Center Settings > Macro Settings (Mac: Excel > Preferences > Security). The choices are:
| Setting | What it means | Recommended? |
|---|---|---|
| Disable VBA macros without notification | Macros never run, you are not told | Too strict for learning |
| Disable VBA macros with notification | Macros are off, the yellow bar lets you enable them per file | Yes, the default and the best choice |
| Disable VBA macros except digitally signed macros | Only code signed by a trusted publisher runs | Good in companies with signed code |
| Enable VBA macros | Every macro in every file runs | No, never |
Under the same dialog you will also find Trusted Locations: folders whose files are always trusted. A sensible setup for this course is to create one folder, for example C:\Excelversity\Macros, add it as a trusted location, and keep your practice files there. Do not add your whole Downloads folder or Desktop.
On a Mac
Excel for Mac supports VBA too, with the same language and almost all of the same objects. The differences are mostly about the operating system: there is no Windows registry, file paths use / instead of \, some dialogs look different, ActiveX controls do not exist, and anything that talks to Windows-only libraries (such as Scripting.FileSystemObject or late-bound Outlook automation) will not work. We point these differences out lesson by lesson.
How the course is organised
The lessons follow one another, and each ends with a short quiz:
- Beginner (lessons 1–12): the editor, the macro recorder, your first macros, variables, ranges, sheets, decisions, loops and text.
- Intermediate (lessons 13–24): dates, dialogs, arrays, your own functions, error handling, events, forms, finding and filtering data and working with tables.
- Advanced (lessons 25–33): charts, pivot tables, files and folders, dictionaries, classes, speed, other Office apps, good practice and a complete project.
Every code sample is complete and safe to run on a blank or sample workbook. Type the samples yourself instead of copying them; your fingers learn the patterns faster than your eyes do.
Common misunderstandings
- "VBA is outdated, I should learn Python instead." Python in Excel is great for analysis, but it runs in the cloud and cannot click buttons, open files or create sheets. VBA is still the tool that automates the Excel desktop application itself, and millions of business workbooks depend on it.
- "A macro can be undone with Ctrl+Z." No. Changes made by VBA clear the undo history. Always test a new macro on a copy of your data.
- "Enable Content makes the file safe." It only means you have decided to trust it. Only enable content in files whose origin you know.
Check yourself
1. Which file type can store VBA macros?
Macro-enabled workbooks use the .xlsm extension (.xlsb, .xltm and .xlam also hold code). Saving as .xlsx strips all VBA.
2. You need a cell that always shows the total of the invoices for one customer. What is the best tool?
Calculations that should stay up to date belong in formulas. A macro would only write a fixed number that goes stale.
3. Which macro security setting is recommended for most people?
With notification, macros are off by default but you can enable them, file by file, for workbooks you trust.
4. A workbook downloaded from the internet shows a red "Security Risk" bar. What is the safe way to run its macros, if you trust the source?
Unblocking removes the "downloaded from the internet" mark for that single file, without lowering security for everything else.
5. Can you undo what a macro did with Ctrl+Z?
Excel cannot undo VBA actions, so test new macros on a copy of your data.