Visual Basic for Applications (VBA) is an event-driven, object-oriented programming language for writing macros, used for the entire Office suite as well as other applications. VBA is not equivalent to VB.NET, or VBS; if you're working in Visual Studio use [vb.net]. If your question is specifically about programming any MS Office application, also use the appropriate tag: [excel], [ms-access], [ms-word], [outlook], [visio], or [ms-project].
Visual Basic for Applications (VBA) is an event-driven programming language which was first introduced by Microsoft in 1993 to give Excel 5.0 a more robust object-oriented language for writing macros and automating the use of Excel. It was introduced to Access, PowerPoint and Word in Office 97. The language and its runtime quickly matured and began being licensed and used in products beyond Microsoft Office applications.
Tag usage
- VBA is not equivalent to VB.NET; if you're working in Visual Studio use vb.net.
- If your question is specifically about programming Excel, Access, Word, Outlook, PowerPoint or Project, also use the appropriate application tag: excel, ms-access, ms-word, outlook, powerpoint or microsoft-project.
- While code written in VBA is typically called a 'macro', the macros tag should not be used for general VBA questions. Please see the macros wiki entry page for more information about correct usage.
- If your code is specific to an Office application (Excel, Word, Outlook, PowerPoint, etc.) then tag it with one of the application-specific tags found below, in addition to this one.
- It is expected that questions tagged vba should either contain VBA code or ask how to use specific VBA code.
Overview
VBA derives from the classic Visual Basic programming language (version 5.0 and later 6.0) and uses a subset of that language's functionality. It is, however, totally dependent on the host application interface for execution and cannot run outside that environment. Unlike classic VB, VBA cannot create independent executables or DLLs. Visual Basic Libraries for interaction with Windows and its services (such as Printers or the Clipboard) are not part of VBA.
VBA 6 was shipped in 1998 and includes a myriad of licensed hosts, among them: Office 2000 - 2007, AutoCAD and PI Processbook. VBA 6 code will run equally well on any host, though the underlying objects native to each host will vary. Though VBA is still built into Microsoft Office applications, its "parent", classic VB, ceased to be an integral of part of Microsoft's development platform when Visual Basic .NET shipped with the first version of the .NET framework in 2002.
VBA has been rarely updated since only including new features to allow it to remain compatible with x64 versions of Windows and Office. Its application-specific object models are regularly supplemented to support new functionality added to the host Office applications. VBA 7 was released in 2010 to address the new 64-bit version of Microsoft Office, which shipped in 2010. There are several important changes made to VBA 7 that make it different from VBA 6, namely compatibility with both 32 and 64-bit versions of Office. Applications using VBA 7 must address both backwards compatibility and 64-bit-safe issues.
In some Starter editions of Office applications, the VBA Editor (VBE) is not available, and programming in VBA cannot be done. VBA support can also be locked out (not installed) by network administrators as a security measure.
It was removed from Office for Mac 2008, however, Microsoft returned VBA to Office 2011. Microsoft has continually been questioned about whether or not VBA will be removed altogether from Office and has repeatedly replied "no", the main reason being a large amount of legacy code in use.
This still holds true, even after the introduction of Visual Studio Tools for Office (VSTO) that allowed the use of C# and VB.NET to automate MS Office, and the JavaScript APIs in Office 2013, although VBA is supported only in the desktop application versions of Office.
VBA inherits much of its syntax from the BASIC programming language, where language features tend to be explicitly expressed in words, e.g. If ... Then ... End If
, Function ... End Function
. It also has many object-oriented features, such as classes and interfaces, and even has some dynamic features in Variant
and Object
types. Below is a simple subroutine, which generates a message box and prints a message to the Immediate window:
Sub HelloWorld()
' This is a comment
Dim message As String
message = "Hello World"
MsgBox message
Debug.Print "I just made a message box that says """ & message & """!"
End Sub
Related Tags:
Frequently Asked Questions:
- How to avoid .Activate and .Select in VBA
- Difference between Visual Basic and VBA
- How do I force VBA/Access to require variables to be defined
- How can I send an HTTP Post request to a server from Excel using VBA?
- Build SQL strings in Access/VBA
- Differences between VBA 6 and VBA 7
Beginner Resources:
References:
Additional Reading:
- VBA Developer's Handbook, 2nd Edition, by Getz & Gilbert
- Professional Excel Development, 2nd Edition