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").Value prints the value and Range("A1").Value = 5 writes one.
  • Debug.Print writes to the Immediate window and costs far less than MsgBox, 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
TypeSizeNotes
Byte1 byte0 to 255
Boolean2 bytesTrue converts to -1, not 1
Integer2 bytes-32,768 to 32,767: too small for row numbers
Long4 bytesThe default choice for counters and row indexes
LongLong8 bytes64-bit Office only; use Long unless you truly need the range
Double8 bytesDefault floating point for arithmetic
Currency8 bytesFixed four decimal places, exact for money
Date8 bytesA Double underneath; day 1 is 1899-12-31
StringvariableFixed-length with String * 10, otherwise dynamic
Variant16 bytes+Empty until assigned; the only type a range read returns
ObjectpointerRequires 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.

Macros, worksheets and ranges Error handling and the classic pitfalls

Last refreshed 2026-09-18.