VBA is a programming language built into Excel that lets you automate repetitive tasks
VBA (Visual Basic for Applications) is a programming tool that lives inside Excel. Instead of clicking the same buttons over and over, you write a small program — called a macro — that does the work for you. To open VBA, you use the Developer tab in Excel's ribbon menu, then click Visual Basic. If you don't see the Developer tab, you need to turn it on first in Excel's settings.
VBA is useful when you have tasks that repeat the same way every time: formatting a spreadsheet, moving data from one sheet to another, or calculating values across hundreds of rows. The time you spend writing the macro comes back to you the first time you run it on a large dataset.
Key Takeaways
- The Developer tab must be enabled in Excel before you can access VBA; it does not appear by default.
- Once the Developer tab is visible, clicking Visual Basic opens the VBA editor where you write and test your code.
- A macro is a recorded or written set of instructions that Excel runs automatically when you trigger it.
- VBA works the same way in Excel on Windows and Mac, though the steps to enable the Developer tab differ slightly between the two.
Enable the Developer Tab on Windows
The Developer tab is hidden by default in Excel. To show it, open Excel and click File in the top left corner. Then click Options (near the bottom of the menu). In the Excel Options window, click Customize Ribbon on the left side.
On the right side of the window, you will see a list of tabs. Look for Developer in that list. If it is not checked, click the checkbox next to it. Then click OK at the bottom. The Developer tab now appears in your ribbon menu next to the View tab.
Enable the Developer Tab on Mac
On a Mac, the process is slightly different. Open Excel and click Excel in the top menu bar, then select Preferences. In the Preferences window, click Ribbon & Toolbar. In the list on the right, find Developer and check the box next to it. Click Save and close the window.
The Developer tab will now appear in your ribbon. On Mac, it usually sits between the View and Design tabs, though the exact position can vary depending on your Excel version.
Open the VBA Editor
Once the Developer tab is visible, click on it. You will see several buttons, including one labeled Visual Basic. Click that button and the VBA editor window opens. This is where you write your code. The editor looks like a text document with line numbers on the left side.
The VBA editor has several panes. On the left is the Project Explorer, which shows the structure of your workbook — the sheets, modules, and other objects. The large area in the middle is where you type your code. At the bottom is the when ready window, which you can use to test small pieces of code. If you do not see these panes, go to the View menu in the VBA editor and click Project Explorer or when ready Window.
Record a Macro Instead of Writing Code
If writing code feels intimidating, you can record a macro instead. Go to the Developer tab and click Record Macro. Excel will ask you to name the macro and choose where to store it. Then perform the actions you want to repeat — click cells, type text, explore formatting, whatever you need. When you are done, click Stop Recording (also in the Developer tab). Excel has now written the VBA code for you based on what you did.
Recorded macros are not always perfect. Excel records every click, including mistakes you might undo. But a recorded macro gives you a starting point. You can then open the VBA editor, look at the code Excel wrote, and clean it up or modify it to work better.
Run a Macro from the Developer Tab
Once you have written or recorded a macro, you run it by clicking the Macros button in the Developer tab. A window opens showing all the macros in your workbook. Click the one you want to run and click Run. Excel executes the code when ready.
You can also assign a macro to a button on your spreadsheet. In the Developer tab, click Insert and choose a button shape. Draw it on your sheet, then right-click it and select Assign Macro. Pick the macro you want and click OK. Now anyone using the spreadsheet can run the macro by clicking that button.
Save Your Work as a Macro-Enabled File
If your spreadsheet contains VBA code, you must save it as a macro-enabled file, not a regular Excel file. When you save, Excel will ask you to choose a file format. Select Excel Macro-Enabled Workbook (the file will end in .xlsm instead of .xlsx). If you save as a regular .xlsx file, your macros will be deleted.
When someone else opens a macro-enabled file, Excel shows a security warning asking whether to enable the macros. This is normal — it is Excel protecting against malicious code. The person opening the file can click Enable Macros if they trust the source.
Frequently Asked Questions
Do I need to know programming to use VBA?
No. You can record a macro by performing actions normally, and Excel writes the code for you. For more complex tasks, learning VBA takes time, but many online resources and tutorials exist. Start with recording, then explore the code to see how it works.
What happens if I close the VBA editor without saving?
Your code is automatically saved when you close the editor. You do not need to save separately. However, if you make changes to your spreadsheet itself, you must save the file (Ctrl+S or Cmd+S) to keep those changes.
Can I share a macro with someone else?
Yes. Save your file as a macro-enabled workbook (.xlsm) and send it to them. When they open it, they will see a security prompt. They can click Enable Macros to use it. Make sure they trust you, since macros can theoretically be used to cause harm.
What if my macro does not work the way I expected?
Open the VBA editor and look at the code. You can add a breakpoint (click in the left margin next to a line) to pause the macro and step through it line by line. This helps you see where it goes wrong. The when ready window lets you test small pieces of code to debug problems.
Can I undo a macro if it changes my data?
Yes, use Ctrl+Z (or Cmd+Z on Mac) to undo. However, undo only works if you have not closed the file. Always save a backup of important spreadsheets before running a new macro for the first time.