VBA editor basics: modules, Subs, and the Immediate window
Watch alongside this article
Public YouTube resources matched to this article's topic, functions, and practice goal.



The Ultimate Excel Advanced Filters In VBA [Full Training Course]
Excel For Freelancers
Open on YouTube (opens in a new tab)Once you’ve recorded a macro or two, the next step is understanding the editor where that code actually lives. It looks intimidating at first — a grey window full of panes — but there are really only a handful of things you need to know to get around.
Opening the editor
Press Alt+F11 from anywhere in Excel. This opens the Visual Basic Editor (VBE) in its own window, separate from the worksheet. Alt+F11 again (or clicking back into the Excel window) switches you back.
The Project Explorer
If you don’t see it, press Ctrl+R. This is the tree view on the left showing every open workbook as a “VBAProject,” and inside each one:
- Microsoft Excel Objects —
ThisWorkbookand one entry per worksheet. Code placed here is tied to that workbook or sheet. - Modules — plain code containers you add yourself. Most regular macros live here.
- Class Modules and Forms — more advanced containers you’ll meet later, for custom objects and dialog boxes.
Inserting a Module
Right-click the VBAProject for your workbook in the Project Explorer, choose Insert → Module, and a blank code window opens. This is where you’ll write most general-purpose macros — anything that isn’t reacting to a specific workbook or worksheet event.
Sub vs Function
Both are procedures — named blocks of code — but they behave differently:
Sub SayHello()
MsgBox "Hello from VBA"
End Sub
A Sub runs a set of actions and doesn’t hand back a value. It’s what
recorded macros always generate, and what you run directly from the
Developer tab or the editor.
Function DoubleIt(n As Double) As Double
DoubleIt = n * 2
End Function
A Function returns a value to whatever called it. Write one in a
standard Module and, as long as it’s not marked Private, it can also be
called directly from a worksheet cell: =DoubleIt(A1).
Running a Sub from the editor
Click anywhere inside a Sub procedure and press F5 (or Run →
Run Sub/UserForm). If your cursor isn’t inside any procedure, VBA asks
which one to run. Subs that require arguments can’t be run this way —
F5 only works for parameterless Subs, which covers the vast majority of
everyday macros.
The Immediate window
Press Ctrl+G (or View → Immediate Window) to open it. It’s a scratchpad for testing things without running a full macro. Type a line and press Enter to execute it immediately:
?Range("A1").Value
The leading ? is shorthand for Print. From inside your code, send
values there with Debug.Print:
Debug.Print "Total is: " & total
This is the single most useful debugging habit in VBA — print values as you go instead of guessing what a variable holds.
Declaring variables
Use Dim to declare a variable before using it, with a type:
Dim total As Double
Dim customerName As String
Dim rowCount As Long
Dim isValid As Integer
Dim dataRange As Range
Dim ws As Worksheet
The types you’ll reach for constantly: String for text, Long for
whole numbers (prefer it over Integer, which has a much smaller range
and no real speed advantage on modern Excel), Double for decimals, and
Range and Worksheet for referring to parts of the workbook itself.
Turn on Option Explicit
At the very top of a module, above any Sub, add:
Option Explicit
This forces every variable to be declared with Dim before it’s used.
Without it, VBA silently creates a new variable the moment you type a
misspelled name — toatl = toatl + 1 runs without complaint, quietly
producing the wrong answer, because toatl and total are two different
undeclared variables to VBA.
Turn this on permanently: Tools → Options → Editor tab → check
“Require Variable Declaration”. Every new module you insert from then
on will have Option Explicit added automatically. It won’t retrofit
modules you’ve already created, so add it by hand to older code.
Go deeper with this skill
Turn the idea into a repeatable Excel workflow you can explain, rebuild, and review later. For this article, the goal is to practice: A tour of the VBA editor for anyone past recorded macros — Project Explorer, modules, Sub vs Function, running code, and Option Explicit.
Practice workbook setup
Create a small practice workbook with one raw-data sheet, one working sheet, and one final output sheet.
Practice workflow
- Rebuild the example once exactly as described, then repeat it with different labels, dates, or amounts.
- Write a short note beside the result explaining what each step is doing and why it matters.
- Change one input value and confirm the output updates in the way you expected.
- Save a clean copy of the workbook before experimenting further.
Quality checks
- Inputs, calculations, and final outputs are separated clearly.
- Headings describe the data without relying on memory or hidden context.
- The final result can be understood by someone who did not build the workbook.
Common mistakes
- Mixing raw data and manual adjustments in the same cells.
- Skipping a quick review after the result looks correct.
- Building a one-off fix instead of a repeatable workflow.
Next actions
- Apply the same pattern to a real workbook with 20 to 50 rows of sample data.
- Add one note that explains when you would not use this approach.
Formula focus: even if this workflow is not formula-heavy, add one check cell that confirms the final output still matches the source data.

