Excel's coding language is called VBA (Visual Basic for Applications), and it lets you automate repetitive tasks, create custom functions, and build interactive tools inside Excel. You don't need to be a programmer to start; you can record simple macros and then read and edit the generated code. This guide walks you through the first steps: enabling the Developer tab, recording a macro, opening the VBA editor, writing and running a simple procedure, and saving your work as a macro-enabled file.
What You'll Need
- A Windows PC or Mac with Microsoft Excel installed (the steps below work for Excel 2016, 2019, 2021, and Microsoft 365; menu names can differ slightly by version).
- A workbook you're willing to experiment on – it's best to use a copy of your data.
- Basic familiarity with Excel menus and saving files.
Step-by-Step
- Enable the Developer tab. In Excel, go to File > Options > Customize Ribbon (on Mac: Excel > Preferences > Ribbon & Toolbar). In the right-hand pane, check the box next to Developer and click OK. The Developer tab now appears in the ribbon.
- Turn on the Macro settings. Still in the Developer tab, click Macro Security (or Macros > Security). Choose Disable all macros with notification – this lets you run macros but Excel will ask first. You don't need to change this to enable recording.
- Record your first macro. On the Developer tab, click Record Macro. Give it a name (no spaces, e.g., FormatReport), assign a shortcut key if you like (Ctrl+Shift+F, for example), and choose where to store it: This Workbook keeps it in the current file. Click OK.
- Perform the actions you want to automate. For example, select a range of cells, set the font to bold, change the fill color, and adjust column widths. Every click and keystroke is being recorded.
- Stop recording. Go back to the Developer tab and click Stop Recording. Your actions have been converted into VBA code.
- Open the VBA editor. Press Alt+F11 (Windows) or Fn+Option+F11 (Mac). You'll see the Project Explorer on the left, which lists your workbook and its sheets.
- Find your recorded macro. In the Project Explorer, expand Modules and double-click Module1. The code you recorded appears in the large white window. It will look something like
Sub FormatReport()...End Sub. - Read the code. Each line that starts with an apostrophe (') is a comment – it's ignored by Excel. Lines like
Range("A1").Selectselect a cell, andSelection.Font.Bold = Truemakes it bold. This is the actual VBA language. - Write your own simple procedure. Below the recorded macro, type the following (you can copy it):
Sub HelloWorld()
MsgBox "Hello from VBA!"End Sub
This is a complete macro that displays a message box. - Run your code. Place your cursor anywhere inside the
HelloWorldsubroutine and press F5 (or click the green Run button in the toolbar). A message box appears. Click OK to close it. - Save as a macro-enabled workbook. Go to File > Save As and choose the file type Excel Macro-Enabled Workbook (*.xlsm). If you save as a regular .xlsx, Excel will strip out your macros with a warning.
- Re-run your macro from Excel. Close the VBA editor (click the X). On the Developer tab, click Macros, select HelloWorld, and click Run. You can also use the shortcut key you assigned earlier.
Common Mistakes
- Forgetting to save as .xlsm. If you save as .xlsx, your macros are deleted. Always check the file extension.
- Using spaces in macro names. VBA doesn't allow spaces – use underscores or capital letters instead (e.g., FormatReport).
- Recording absolute references when you want relative. By default, the macro recorder uses absolute cell references (it always goes to the exact cell you clicked). If you want the macro to work on whatever cell is selected, change the reference style to Relative Reference (a button next to Record Macro) before recording.
- Not closing the VBA editor properly. If you leave the editor open and try to run a macro from Excel, you might get a “Can't execute code in break mode” error. Close the editor or press Ctrl+Break to reset.
- Editing code while it's running. If you see a yellow highlight in the VBA editor, the macro is paused. Press F5 to continue or Ctrl+Break to stop.
Tips
- Use the macro recorder as a learning tool. Record a task, then read the generated code – it's the fastest way to learn VBA syntax.
- Comment your code. Write a line starting with an apostrophe to explain what each part does. You'll thank yourself later.
- Keep macros small. A macro that does one thing well is easier to debug and reuse than a giant one that does everything.
- Test on a copy. Macros can't be undone with Ctrl+Z after they run. Always test on a copy of your data.
- Use named ranges. Instead of selecting cells, you can reference a named range directly in VBA, e.g.,
Range("SalesData").Font.Bold = True. This makes your code more robust. - Learn to use the Immediate Window. In the VBA editor, press Ctrl+G to open it. You can type
?Range("A1").Valueand press Enter to see the current value of a cell – great for debugging.
Troubleshooting
Macro doesn't appear in the list. Make sure you saved the file as .xlsm and that the macro is stored in This Workbook, not Personal Macro Workbook. If it's in the personal workbook, it's only available when that file is open.
“Macros are disabled” error. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and select Disable all macros with notification. Then close and reopen the workbook and click Enable Content when the yellow bar appears.
VBA editor opens but the code is grayed out. This usually means the project is protected or the workbook is in a read-only mode. Check if the file is marked as final or protected – go to File > Info and look for Protect Workbook.
“Compile error: Sub or Function not defined.” This means VBA can't find a subroutine you called. Check the spelling and make sure the subroutine actually exists in a module.
Macro runs but doesn't do anything. If you recorded absolute references, it might be operating on the wrong cells. Switch to relative reference and re-record, or edit the code to use ActiveCell instead of a fixed range.
Excel crashes when running a macro. This is rare but usually caused by an infinite loop. If your code has a Do While or For loop, make sure it has an exit condition. Press Ctrl+Break to stop a runaway loop.
Frequently Asked Questions
Is VBA the same as Excel formulas?
No. Formulas are written in cells and recalculate automatically. VBA is a programming language that runs behind the scenes to automate tasks, manipulate the workbook, and respond to events. You can use both together – for example, a macro that inserts a formula into a cell.
Can I use VBA on Excel for Mac?
Yes, Excel for Mac supports VBA, but the keyboard shortcuts differ (e.g., Alt+F11 becomes Fn+Option+F11). Some Windows-only features like ActiveX controls are not available on Mac, but basic macros work fine.
Do I need to learn a different language for Excel Online?
Excel for the web does not support VBA macros. If you need automation in the browser, you'd use Office Scripts, which is a different language (TypeScript-based). VBA only works in the desktop versions of Excel.
Is VBA safe to run?
Macros can contain harmful code, so only enable macros from sources you trust. Excel's default security settings will warn you before running macros from unknown files. Never open a macro-enabled file from an unsolicited email.
Can I share my macro with someone else?
Yes, send them the .xlsm file. They'll need to enable macros when they open it. If they have a different Excel version, the code usually still works, but some features might differ.
How do I delete a macro?
Open the VBA editor, find the module containing the macro, right-click the module in the Project Explorer, and choose Remove Module. Or, in Excel, go to the Developer tab, click Macros, select the macro, and click Delete.