Excel VBA (Visual Basic for Applications) lets you record and write small programs, called macros, that automate repetitive spreadsheet tasks. You can get started in a few minutes: enable the Developer tab, record a simple macro, then open the code and edit it. No programming background is needed for this tutorial, just a desktop copy of Excel for Windows or Mac.
What You'll Need
- A desktop version of Microsoft Excel (the VBA editor is not available in Excel for the web or the mobile apps)
- A workbook you can safely experiment with, ideally a copy of a real file so mistakes cost nothing
- Ten to fifteen minutes of uninterrupted time
Step-by-Step
- Enable the Developer tab. Right-click anywhere on the ribbon and choose Customize the Ribbon (on Mac: Excel menu > Preferences > Ribbon & Toolbar). Tick Developer in the list on the right and click OK. The Developer tab now appears in the ribbon. Menu labels can differ slightly between Excel versions, so if a label doesn't match exactly, look for the closest equivalent.
- Allow macros to run. Open the Developer tab and click Macro Security. Choose the option to disable macros with notification (not the option to enable all macros), then click OK. This means Excel will warn you before running macros from files you open, which is the safe default.
- Record a macro. On the Developer tab, click Record Macro. Give it a short name without spaces, such as FormatHeader, choose This Workbook under Store macro in, optionally assign a shortcut key, and click OK. From this point, Excel records your actions.
- Perform the actions you want to automate. For example: select cells A1 to D1, make them bold, fill them with a colour, and centre the text. Keep the actions simple for your first attempt.
- Stop recording. Click Stop Recording on the Developer tab (or the small square stop icon in the bottom-left corner of the sheet). Your macro is now saved in the workbook.
- Open the VBA editor to see the code. Press Alt+F11 (on Mac: Option+F11 or Tools > Macro > Visual Basic Editor). In the Project window on the left, expand Modules and double-click Module1. You'll see your recorded actions translated into VBA code, starting with Sub FormatHeader() and ending with End Sub.
- Read the code. Each line is one action you performed: Range selections, .Bold = True, .Color settings and so on. Notice that the recorder is literal; it records every selection and click, even the ones that weren't needed.
- Tidy the recorded code. Delete any lines that merely select cells when a later line already specifies the range. For example, Selection.Font.Bold = True applied after a select can usually become Range("A1:D1").Font.Bold = True on its own. This is the core habit of VBA: work with objects directly instead of selecting first.
- Run the macro. Go back to the worksheet, click somewhere to undo your manual formatting, then press Alt+F8, select FormatHeader and click Run (or press Alt+F8 and use the Run button; on Mac use Tools > Macro > Macros). The formatting should reappear instantly.
- Save the workbook in a macro-enabled format. Choose File > Save As and pick Excel Macro-Enabled Workbook (*.xlsm). A normal .xlsx file silently strips out all VBA code when saved, so this step matters.
- Write a small macro by hand. Back in the VBA editor, type a new block:
Sub AddTotal()
Range("E1").Value = "Total"
End Sub
Run it with F5 while the cursor is inside the block. Anything you can express as an object (a range, a sheet, a cell) followed by a property or method can be written this way, which is the foundation of everything beyond recording.
Common Mistakes
- Saving as .xlsx. The code is discarded without warning. Always use .xlsm for workbooks containing macros.
- Relying on recorded Select lines. Recorded macros select cells before acting on them. Selection-based code is slow and breaks if the user clicks elsewhere; rewrite it to reference ranges directly.
- Enabling all macros permanently. This opens the door to malicious macros in files you receive. Keep the notification setting on and only enable macros for files you trust.
- Not testing on a copy. A macro that deletes or overwrites data cannot be undone with Ctrl+Z. Always run new code on a copy of the workbook.
- Macros stored in Personal Workbook vs This Workbook. Macros stored in This Workbook only run when that file is open; if a macro seems to have vanished on another file, check where it was stored.
Tips
- Use the macro recorder as a learning tool even after you can write code: record an action you don't know the syntax for, then read the generated code.
- Press F1 with the cursor on any VBA keyword to open Microsoft's documentation for it.
- Use Option Explicit at the top of a module. It forces you to declare every variable, which catches typos that would otherwise create silent new variables.
- Add comments with an apostrophe (') so future-you understands what each block does.
- Learn the Immediate window (Ctrl+G in the editor): type ?Range("A1").Value and press Enter to inspect anything without writing a full macro.
Troubleshooting
- Macros are disabled and won't run. When you open the file, click Enable Content in the yellow security bar. If there's no bar, check Developer > Macro Security is set to notification, close and reopen the file.
- The macro does nothing or errors out. Press Alt+F8, select the macro and click Step Into, then press F8 line by line to see exactly where it fails. Read the error message; Run-time error 1004 usually means a range or sheet name is wrong.
- Alt+F11 does nothing. On some keyboards you need Fn+Alt+F11. Alternatively use Developer > Visual Basic.
- The macro works on one sheet but not another. Recorded code often references the sheet by name. Check the code for Sheets("...") references and adjust them, or qualify your ranges with the intended sheet name.
- Buttons or shortcuts stopped working. If the workbook was saved as .xlsx, the code is gone; recover from a previous .xlsm version or re-record.
- Code runs but is slow. Remove Select and Activate statements, and turn off screen updating at the start of long macros with Application.ScreenUpdating = False, restoring it to True at the end.
Frequently Asked Questions
Is VBA still supported in Excel?
Yes. VBA is fully supported in desktop Excel for Windows and Mac. It is not available in Excel for the web or the mobile apps.
Do I need to know programming to use VBA?
No. The macro recorder generates working code from your clicks and keystrokes, and you can gradually edit that code to learn the syntax.
Is VBA the same as macros?
A macro is the recorded or written procedure; VBA is the language macros are written in. In practice people use the terms interchangeably.
Why does Excel warn me about macros?
Macros can run harmful code, so Excel blocks them in files from other people until you explicitly enable them. Only enable macros in files from sources you trust.
Can I undo a macro's changes?
Usually not. Excel's undo history is cleared when a macro runs, so test new macros on a copy of your file.
Can I run a macro automatically when a workbook opens?
Yes. Place your code in a procedure named Workbook_Open in the ThisWorkbook module, found under Microsoft Excel Objects in the VBA editor's Project window.