You can write VBA in Excel today without any prior programming: open the Visual Basic Editor, record a simple macro, look at the code Excel wrote for you, and then edit one line to see what changes. That record-then-edit loop is the fastest way beginners learn, because Excel generates working code first and you only reverse-engineer it afterwards. Nothing extra needs installing — VBA ships inside Excel on Windows, and on Mac it is also built in, with a few limitations covered below.
What You'll Need
- Excel for Microsoft 365, Excel 2021/2019, or Excel 2016 on Windows or Mac. The web version of Excel (in a browser) does not include the Visual Basic Editor, so use the desktop app.
- A workbook to practise on — preferably a copy of your real file, not the original.
- Currently, the Developer tab visible in the ribbon (steps below show how to enable it).
- On Windows: the default setting already allows VBA. On Mac, no extra setup is needed for recording, but code using Windows-only features may not run.
Step-by-Step
- Save a backup copy first. Use File > Save As and keep a separate practice file, because macros change your workbook permanently and there is no general undo for a macro run.
- Enable the Developer tab. Right-click any empty area of the ribbon and choose Customize the Ribbon. In the right-hand list, tick Developer and click OK. Menu labels can differ slightly by version and platform; on Mac, look under Excel > Preferences > Ribbon & Toolbar if you prefer to enable it there.
- Open the Visual Basic Editor. Click Developer > Visual Basic, or press Alt+F11 on Windows. On Mac the shortcut is Option+F11 or Fn+Option+F11 depending on your keyboard. A separate grey window opens — that is the editor. If you see nothing, check that you are in the desktop app, not a browser tab.
- Record a macro to see code appear. Back in Excel, click Developer > Record Macro. Give it a name with no spaces (for example FormatSheet), leave “Store macro in” as This Workbook, and click OK. Now do something visible: select a range, make it bold, change the fill colour, set a column width. Then click Developer > Stop Recording.
- Read the generated code. Press Alt+F11 again. In the editor's left panel (Project Explorer) find your workbook, expand Modules, and double-click Module1. You should see a Sub with your macro name and lines such as Selection.Font.Bold = True. Do not worry about every word yet — just notice that each action you took became one line.
- Edit a single line. Find the line setting a colour or width and change the value. For example, change a number like 14 to 20, or swap True for False. Click anywhere inside the Sub and press F5 to run it. Watch Excel apply your edited version.
- Write your first line from scratch. In the editor, click Insert > Module to add a fresh module. Type this, exactly, including the dot:
Sub HelloCell()Range("A1").Value = "Hello"End Sub
Press F5. Cell A1 on the active sheet now contains the word Hello. The indentation is cosmetic but makes code readable. - Understand the three building blocks. A Sub is a container for instructions. An object is a thing (a Range, a Worksheet, the Workbook). A property is a characteristic you read or set (Value, Font.Bold). A method is an action (Select, Copy, Delete). Most beginner code is simply Object.Property = value or Object.Method.
- Refer to sheets explicitly. Avoid relying on whichever sheet happens to be active. Change the line to
Worksheets("Sheet1").Range("A1").Value = "Hello". Run it while a different sheet is active and see that it still writes to Sheet1. This habit prevents most “wrong sheet” bugs. - Run macros from buttons. Right-click the Quick Access Toolbar, choose Customize, add Macros to it so your macro appears as a one-click button. Alternatively, assign a macro to a shape: Insert > Shapes, draw one, right-click it, choose Assign Macro, and pick your Sub.
- Delete the practice macro cleanly. Developer > Macros lists everything. Select a macro and click Delete to remove it. Deleting the module (right-click Module1 in the editor > Remove Module1) removes all macros stored in it — only do that once you are sure.
- Save in a macro-enabled format. Use File > Save As and choose Excel Macro-Enabled Workbook (.xlsm). If you save as ordinary .xlsx, Excel strips the code and warns you on close. Save as .xlsm every time.
- Practice a small real task. Record yourself applying a standard header format to a report: bold, fill colour, freeze the top row. Then edit the recorded code to add one extra line, such as
Range("A1").EntireRow.AutoFit. That one edit turns a recording into a reusable tool.
Common Mistakes
- Saving as .xlsx. The most frequent beginner loss. Code vanishes because the file format cannot hold macros. Always .xlsm.
- Relying on Selection. Recorded macros are full of Selection and ActiveSheet. They break the moment you click somewhere else. Replace with explicit sheet and range references.
- Using spaces or starting with a number in macro names. Names must start with a letter and contain no spaces. Underscores are fine.
- Missing End Sub or End If. Every block opened must be closed. The editor highlights the offending line in red or yellow when you try to run.
- Typing strings without quote marks. Text values need double quotes:
Range("A1").Value = "Hello". Without them, VBA treats the word as a variable name. - Forgetting a dot before a property.
Range(“A1”)Valueis a syntax error; it must beRange("A1").Value. - Running destructive macros on live data. Anything that deletes rows or clears sheets runs instantly with no undo. Test on a copy.
Tips
- Use the Macro Recorder as a dictionary, not a solution. Record an action, read the code, then rewrite it cleanly.
- Turn on the Immediate window (View > Immediate Window) and type
?Range("A1").Valueto check a value without running a whole macro. - Press F8 to step through a macro one line at a time — the single most useful debugging habit.
- Add
On Error Resume Nextonly sparingly; it hides problems. Prefer fixing the cause. - Comment your code with an apostrophe:
'this formats the header row. Future you will thank present you. - Keep one module per purpose (formatting, importing, reporting) rather than one giant module.
Troubleshooting
- “Cannot run the macro… The macro may not be available” — the workbook lost its code (saved as .xlsx), the macro is in a different workbook than you think, or macros are disabled. Check the file extension first.
- A yellow warning bar about macros — click Enable Content if you trust the file's source. Only enable macros in files you or someone you trust created; never enable in unknown downloads.
- Compile error, red highlighted line — read the highlighted line for a missing quote, bracket, or End statement. Press F8 to step and see where it stops.
- “Object required” or “Subscript out of range” — usually a sheet or workbook name in your code does not match the real tab name, including stray spaces. Compare exactly.
- Nothing happens when you run it — the macro may be writing to a sheet you are not looking at, or the values are already set to what the code assigns. Check with the Immediate window.
- Macro list is empty — set “Macros in” to All Open Workbooks; macros stored in a personal workbook or another file will not show under the current sheet only.
Frequently Asked Questions
Is VBA available in Excel on Mac?
Yes, desktop Excel for Mac includes the Visual Basic Editor and the recorder, but some Windows-specific features and APIs are not supported, so code written on Windows may need adjusting.
Does Excel in a browser support VBA?
No. The web version cannot open the editor or run macros. Open the file in the desktop app instead.
Is learning VBA still worth it?
For automating repetitive tasks inside Excel, yes — it is built in, needs no extra install, and macro recording gives you working examples to learn from.
Why did my code disappear after saving?
You saved as .xlsx. The macro-enabled format .xlsm is required to keep VBA code in the file.
Can I undo a macro run?
Generally no. Macros bypass Excel's undo stack, which is why testing on a copy and saving .xlsm backups matters.
Are macros a security risk?
They can be, since VBA can perform file and system actions. Only enable macros in files from trusted sources and keep the default macro security settings in the Trust Center.