The VBA editor and the language
Finding the VBE, what a macro-enabled file really is, and the declarations and types that keep a module honest.
Getting into the editor
- Alt + F11 opens the Visual Basic Editor (VBE) from any Office application; Ctrl + G shows the Immediate window.
- F5 runs the procedure the cursor is in, F8 steps one line at a time, and hovering over a variable shows its current value while paused.
- Code lives in the document: press Alt + F11, use Insert > Module for a standard module, and the file must be saved as .xlsm (macro-enabled workbook) or .xlsb.
- The Immediate window runs single statements as you type them:
?Range("A1").Valueprints the value andRange("A1").Value = 5writes one. Debug.Printwrites to the Immediate window and costs far less thanMsgBox, which stops execution until you click OK.
A workbook opened from the internet or an email attachment is blocked by Protected View and by the Mark of the Web, and macros stay disabled until the file is unblocked. That is a security boundary, not a bug: enable macros per file after checking the source, or trust a folder you control.
Put reusable code in a standard module of the Personal.xlsb workbook, stored in the XLSTART folder, rather than in every workbook. It loads invisibly at start-up and keeps the macros out of files you share.
Declarations and types
Option Explicit ' force every variable to be declared: always keep this
' A standard module holds Subs (do something) and Functions (return a value).
Private Const MAX_ROWS As Long = 1000
Public Function FormatName(ByVal first As String, ByVal last As String) As String
Dim full As String
full = Trim$(first) & " " & Trim$(last) ' & concatenates text
FormatName = full ' return by assigning to the name
End Function
Public Sub Demo()
Dim i As Long ' Long for counters, never Integer
Dim label As String
Dim started As Date
Dim amount As Currency
started = DateSerial(2026, 9, 18) ' DateSerial avoids locale issues
amount = 19.99@ ' @ is the Currency type suffix
For i = 1 To 3
label = FormatName("Ada", "Lovelace")
Debug.Print i; label; Format$(started, "yyyy-mm-dd"); amount
Next i
' Set is required for object references, Let is implied for values
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets(1)
Debug.Print ws.Name
End Sub| Type | Size | Notes |
|---|---|---|
Byte | 1 byte | 0 to 255 |
Boolean | 2 bytes | True converts to -1, not 1 |
Integer | 2 bytes | -32,768 to 32,767: too small for row numbers |
Long | 4 bytes | The default choice for counters and row indexes |
LongLong | 8 bytes | 64-bit Office only; use Long unless you truly need the range |
Double | 8 bytes | Default floating point for arithmetic |
Currency | 8 bytes | Fixed four decimal places, exact for money |
Date | 8 bytes | A Double underneath; day 1 is 1899-12-31 |
String | variable | Fixed-length with String * 10, otherwise dynamic |
Variant | 16 bytes+ | Empty until assigned; the only type a range read returns |
Object | pointer | Requires Set, never plain assignment |
' Procedures, scope and the argument-passing default
Public Sub Process(ByVal path As String, ByRef log As Collection)
' ByVal passes a copy; ByRef passes a reference and can change the caller
Static calls As Long ' Static keeps its value between calls
calls = calls + 1
log.Add path
End Sub
' Enums group magic numbers into named constants
Public Enum Status
stIdle = 0
stRunning = 1
stFailed = -1
End Enum
' Type is a value record; Class is a reference type with methods
Private Type Point
x As Double
y As Double
End Type⚠️
Arguments are
ByRef by default, so a called procedure can silently overwrite a variable in the caller. Pass ByVal for everything you do not intend to return, and write ByRef explicitly when you do.FAQ
Why is my macro disabled when I open the file?
Protected View or the Mark of the Web is blocking macros because the file came from outside your trusted locations. Right-click the file, choose Properties, and select Unblock, or add the folder to the Trust Center's trusted locations.
Should I use Option Explicit?
Always. Without it a typo creates a new empty Variant instead of raising an error, so
total = totl + 1 runs happily and produces the wrong answer. In the VBE, enable Require Variable Declaration so every new module gets it automatically.Related
Macros, worksheets and ranges Error handling and the classic pitfalls
Last refreshed 2026-09-18.