Part of the free Module 12: Excel VBA Course · Lesson 1 of 18 · Full Excel course
Updated on 5 September 2026 · Works in Excel 365, 2021, 2019 and 2016 unless noted.
This Excel VBA introduction explains what Visual Basic for Applications is and gets you writing code in ten minutes. VBA is the programming language built into Excel that automates repetitive work, builds custom functions and creates complete data-entry systems. You will enable the Developer tab, tour the Visual Basic Editor, insert a module, write and run your first Sub procedure, save the file as .xlsm and set macro security correctly.
What is VBA and why learn it?
VBA (Visual Basic for Applications) is the scripting language shared by Excel, Word, PowerPoint, Outlook and Access. In Excel it can do anything you can do with the mouse and a great deal more: format hundreds of reports in seconds, loop through every sheet in a workbook, pull data from a database, send emails from Outlook or show a custom form. The code is stored inside the workbook and runs on any Windows or Mac copy of Excel from 2016 through Microsoft 365, with no installation and no admin rights.
A piece of VBA code that performs a task is called a macro. You can record a macro with the mouse (covered in the next lesson) or write it by hand in the Visual Basic Editor. Writing by hand is what this course teaches, because recorded code is verbose and cannot make decisions, repeat itself or ask the user a question.
How to enable the Developer tab in Excel
The Developer tab holds the Visual Basic, Macros, Record Macro and form-control buttons. Excel hides it by default, so switch it on once and it stays on for every workbook.
- Click File > Options.
- Select Customize Ribbon in the left pane.
- In the right-hand list under Main Tabs, tick Developer.
- Click OK. The Developer tab appears at the right end of the ribbon.

On a Mac the setting is under Excel > Preferences > Ribbon & Toolbar. The short video below shows the same steps.
How to open the Visual Basic Editor
On the Developer tab the Visual Basic button is the first item in the Code group. Click it, or press Alt+F11 from anywhere in Excel, to open the Visual Basic Editor (VBE). Alt+F11 also switches you back to the workbook, so you can flip between the two windows without touching the mouse.

A tour of the Visual Basic Editor
The Visual Basic Editor is a separate program window with its own menus. Four panes matter from day one. If any of them is missing, open it from the View menu or with the shortcut in the table.
| Window | Shortcut | What it does |
|---|---|---|
| Project Explorer | Ctrl+R | Tree of every open workbook, its sheet objects, ThisWorkbook, modules, user forms and class modules. Double-click an item to open its code. |
| Properties window | F4 | Settings of the selected object. Use it to rename a module or change a sheet’s Visible property. |
| Code window | F7 | The editor where you type procedures. The two drop-downs at the top jump between objects and procedures. |
| Immediate window | Ctrl+G | Runs a single line at once. Type ?Range("A1").Value and press Enter to read a cell; type Range("A1").Value = 5 to write one. |
| Locals and Watch windows | View menu | Show variable values while code is paused. Covered in the debugging lesson. |

Before you write anything, choose Tools > Options and tick Require Variable Declaration. From then on every new module starts with Option Explicit, which forces you to declare variables and catches misspelt names before they cause silent bugs. Every code sample in this course assumes that line is at the top of the module.
Insert a module and write your first Sub
Code lives in a module, not on a sheet. In the VBE choose Insert > Module; a blank code window named Module1 opens under a Modules folder in the Project Explorer. Type the procedure below exactly as shown.
Option Explicit
Sub Hello_VBA()
MsgBox "Hello from VBA!", vbInformation, "My first macro"
End Sub
Every macro starts with Sub followed by a name and a pair of brackets, and ends with End Sub. The name may contain letters, numbers and underscores but no spaces. MsgBox displays a pop-up message; vbInformation adds the blue “i” icon and the last argument is the dialog title.
There are three ways to run it:
- Click anywhere inside the procedure and press F5 (or Run > Run Sub/UserForm).
- Return to Excel, press Alt+F8, select Hello_VBA and click Run.
- On the Developer tab click Insert > Button (Form Control), draw a button on the sheet and assign the macro to it.
Worked example: write a report header to the sheet
A message box proves the code runs; the next macro changes the worksheet. Start with a blank sheet, paste the procedure into Module1 below Hello_VBA and press F5.
Sub Write_Report_Header()
Dim sh As Worksheet
Set sh = ThisWorkbook.Worksheets(1)
sh.Range("A1").Value = "Report Date"
sh.Range("B1").Value = Date
sh.Range("A2").Value = "Prepared by"
sh.Range("B2").Value = Application.UserName
sh.Range("A1:A2").Font.Bold = True
sh.Columns("A:B").AutoFit
MsgBox "Header written to " & sh.Name, vbInformation
End Sub
Result on the first worksheet:
| A | B | |
|---|---|---|
| 1 | Report Date | 05/09/2026 |
| 2 | Prepared by | Your Office user name |
Line by line: Dim sh As Worksheet declares a variable that will hold a sheet; Set points it at the first worksheet of the workbook that contains the code; the Range lines write values; Date and Application.UserName are built-in values; AutoFit widens the columns. Change a cell address or text, press F5 again and the sheet updates instantly.
Save as .xlsm and set macro security
A workbook that contains code must be saved as Excel Macro-Enabled Workbook (.xlsm) or Excel Binary Workbook (.xlsb). Press F12, change Save as type and click Save. Saving as .xlsx strips every macro without warning beyond one small prompt, and there is no way to recover them afterwards.
When you reopen the file Excel shows a yellow security bar; click Enable Content to allow the code to run. The behaviour is controlled by Developer > Macro Security (File > Options > Trust Center > Trust Center Settings > Macro Settings).
| Macro setting | Behaviour | Use when |
|---|---|---|
| Disable VBA macros without notification | Macros never run, no prompt | Locked-down corporate PCs |
| Disable VBA macros with notification (default) | Yellow bar asks you to Enable Content each time | Recommended for everyone |
| Disable except digitally signed macros | Runs only code signed with a trusted certificate | Companies that sign their tools |
| Enable VBA macros | Everything runs silently | Never; one malicious download can run code |
Keep the default and add the folder where you keep your own projects as a Trusted Location in the same Trust Center dialog; files there open without the prompt. Files downloaded from the internet carry Mark of the Web and since 2022 Excel blocks their macros outright with a red bar. Right-click the file in File Explorer, choose Properties, tick Unblock and click OK before opening it.
Tips and common mistakes
- Turn on Require Variable Declaration before writing your first line. Option Explicit is the cheapest bug-prevention tool in VBA.
- Put code in a standard module, not in a sheet object, unless it is an event procedure. Sheet modules cannot be called from the Alt+F8 dialog by default.
- Save before running anything that deletes or overwrites data. Macros cannot be undone with Ctrl+Z.
- Use F8 to step through a procedure one line at a time and watch the sheet change behind the editor.
- Name procedures descriptively (Write_Report_Header, not Macro1) and keep one module per topic; rename modules in the Properties window.
- ThisWorkbook versus ActiveWorkbook: ThisWorkbook is always the file holding the code; ActiveWorkbook is whichever window is in front. Beginners often write to the wrong file by using the second.
- Mac users open the editor with Fn+Alt+F11 and use View > Immediate Window if Ctrl+G does nothing.
Errors and how to fix them
| Message | Cause | Fix |
|---|---|---|
| Macros have been disabled | Workbook opened with the default security setting | Click Enable Content, or store the file in a Trusted Location |
| Microsoft has blocked macros from running because the source of this file is untrusted | Mark of the Web on a downloaded file | File Properties > Unblock, then reopen |
| Compile error: Variable not defined | Option Explicit is on and a variable was not declared (often a typo) | Add a Dim line or correct the spelling |
| The macro may not be available in this workbook or all macros may be disabled | Name typed wrongly in Alt+F8, or code is in a sheet module | Check the name; move the Sub to a standard module |
| The following features cannot be saved in macro-free workbooks | Saving an .xlsm as .xlsx | Click No and choose Excel Macro-Enabled Workbook |
| Run-time error 9: Subscript out of range | Sheet or workbook name in the code does not exist | Check spelling and that the file is open |
Practice exercise
- Enable the Developer tab, open the Visual Basic Editor with Alt+F11 and dock the Project Explorer and Properties window if they are hidden.
- Insert a module, rename it modIntro in the Properties window and type Hello_VBA. Run it with F5, then again with Alt+F8.
- Copy Write_Report_Header, change it so the header lands in D1:E2 and the “Prepared by” line shows your own name as text.
- In the Immediate window type
?ThisWorkbook.Worksheets.Countand thenWorksheets(1).Range("A5").Value = "Test". Check the sheet. - Save the file as .xlsm, close it, reopen it and enable content. Confirm both macros still appear in Alt+F8.
Key takeaways
- VBA is built into every desktop Excel and automates anything you can do by hand, plus forms, events and Outlook.
- Enable the Developer tab once via File > Options > Customize Ribbon; open the editor with Alt+F11.
- The Project Explorer, Properties, Code and Immediate windows are the four panes you use every day.
- Code goes in a module inserted with Insert > Module; every macro is a Sub … End Sub block run with F5 or Alt+F8.
- Save as .xlsm or .xlsb, keep the default macro security and use Trusted Locations for your own projects.
- Switch on Require Variable Declaration so Option Explicit protects every module.
Related lessons
- Excel VBA course hub (all lessons in order)
- Record a macro in Excel
- VBA variables and data types
- MsgBox and InputBox
- Debugging VBA with breakpoints and the Immediate window
- Excel dashboard course (where VBA powers the finished product)
- 300+ ready VBA projects
- Getting started with VBA in Office (Microsoft Learn)
Frequently asked questions
Is VBA still worth learning in 2026?
Yes. VBA ships with every desktop version of Excel, needs no installation or admin rights, and remains the only way to automate the Excel interface itself: formatting, printing, user forms, events and Outlook email. Office Scripts run only in Excel for the web and Python in Excel handles analysis, not automation, so neither replaces it.
What is the shortcut to open the Visual Basic Editor?
Alt+F11 opens the Visual Basic Editor from Excel and switches back again. Alt+F8 opens the Macro dialog to run a macro, Ctrl+G shows the Immediate window inside the editor, Ctrl+R shows the Project Explorer and F5 runs the procedure under the cursor.
Why do my macros disappear after saving?
The workbook was saved as .xlsx, which cannot store code. Excel warns once with the “features cannot be saved in macro-free workbooks” prompt; if you click Yes the modules are gone. Save as .xlsm (macro-enabled) or .xlsb (binary) instead and the code is kept.
What is the difference between a macro and VBA?
VBA is the language; a macro is a procedure written in that language, usually a Sub that a user runs. Recording a macro makes Excel write VBA for you. Everything the recorder produces is VBA, but hand-written VBA can also loop, test conditions, use variables and show forms, which the recorder cannot.
Do I need to enable macros every time I open the file?
With the default setting, yes, unless the file is in a Trusted Location or you tick “Make this file a trusted document” on the security bar. Add your own project folder as a Trusted Location under File > Options > Trust Center > Trust Center Settings > Trusted Locations and the prompt disappears for files stored there.
Watch the step-by-step video tutorial
Want the finished version? Ready-made Excel dashboards, trackers and VBA systems are available at NextGenTemplates.com.