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
Sub Combinations()
Dim xs(1 To 3) As Long
xs(1) = 2: xs(2) = 3: xs(3) = 5
Dim total As Long, i As Long
For i = LBound(xs) To UBound(xs)
total = total + xs(i)
Next i
Dim box As New Collection
box.Add total, "sum"
Debug.Print box("sum") & " / " & xs(UBound(xs)) ' 10 / 5
End Sub
' Counter.cls — a class module: state behind a property and a method.
Private m_count As Long
Public Property Get Value() As Long
Value = m_count
End Property
Public Sub Increment(Optional ByVal step As Long = 1)
m_count = m_count + step
End Sub
' A standard module consuming it.
Sub Demo()
Dim c As Counter: Set c = New Counter
c.Increment 2: c.Increment
Debug.Print c.Value ' 3
End Sub
' A worksheet module: the host calls this whenever a cell changes.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 2 Then
Target.Offset(0, 1).Value = UCase(Target.Value)
End If
End Sub
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 Explicitmakes declaration compulsory and catches typos at compile time, butVariantstays the default, so late binding is the fallback for untyped members.- Object-oriented without inheritance:
Implementsmaps 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 = Nothingdrops 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
- Official sitelearn.microsoft.com
- [MS-VBAL]: VBA Language Specification — Microsoft Open Specificationslearn.microsoft.com
- [MS-OVBA]: Office VBA File Format Structure — Microsoft Open Specificationslearn.microsoft.com
- [MS-OVBA] 1.1 Glossary — VBA host application, VBA projectlearn.microsoft.com
- Implements statement (VBA) — Microsoft VBA language referencelearn.microsoft.com
- Programming with the C API in Excel — Microsoft Learn (XLM superseded by VBA; VBA sheets introduced with Excel version 5)learn.microsoft.com
- Getting started with VBA in Office — Microsoft Learnlearn.microsoft.com
- Data type summary (VBA) — Microsoft VBA language referencelearn.microsoft.com
- 64-bit Visual Basic for Applications overview — Microsoft Learnlearn.microsoft.com
- Rules for Managing Reference Counts — Windows COM (Microsoft Learn)learn.microsoft.com
- Macros from the internet are blocked by default in Office — Microsoft 365 Appslearn.microsoft.com
- Microsoft Announces Widespread Licensing and Availability of Visual Basic, Applications Edition (1996)news.microsoft.com
- The History of Microsoft — 1994 (Excel 5.0 listed among the year's releases)learn.microsoft.com