All languages

Visual Basic for Applications dialect

Microsoft's hosted dialect of Visual Basic: a macro and automation language that runs only inside a host application. It superseded Excel's XLM macro language and made each host's object model the standard library.

Overview

VBA is Microsoft's hosted dialect of Visual Basic: an event-driven macro and automation language that runs only inside a host application. It replaced the XLM macro language as Excel's way to automate a workbook, and Microsoft publishes an implementation-independent specification for it even though its libraries belong to the host.

Code

Option Explicit
Sub Primitives()
    Dim n As Long, s As String
    n = 42: s = "Hello"
    Dim ratio As Double: ratio = n / 2
    Debug.Print s & ", " & ratio         ' Hello, 21
End Sub
Construction and consumption, then the host's own entry point. Set … = New binds a class-module instance and Property Get reads it back; Worksheet_Change is never called from code — Excel raises it by naming convention, which is also how the macro recorder emits procedures.

Distinctive traits

  • Hosted by definition: code runs only in an application implementing a VBA environment, so a solution ships as a document, never an executable.
  • The host's object model is the standard library — programming means properties, methods and event procedures on objects such as Range.
  • A module stream holds source beside a version-specific compiled cache that readers must ignore; the host's own interpreter runs the project.
  • Option Explicit makes declaration compulsory and catches typos at compile time, but Variant stays the default, so late binding is the fallback for untyped members.
  • Object-oriented without inheritance: Implements maps class members onto a type-library interface, and COM supplies the polymorphism.
  • COM reference counting holds an object until the last reference goes out of scope; Set x = Nothing drops one reference early.

Status

Active: VBA 7 shipped with Office 2010 and 7.1 with Office 2013, adding LongPtr for 32- and 64-bit hosts. Office has blocked macros in files from the internet by default since 2022.

History

VBA first shipped inside Excel 5.0, taking over from the macro-sheet XLM language it has "long superseded". Microsoft's yearly company history lists Excel 5.0 under 1994 while other accounts say 1993, so the release year is left blank here. Microsoft began licensing VBA to other vendors in 1996.

Sources & references