A message from our CEO about the future of Stack Overflow and Stack Exchange. Read now.

Tag Info

Visual Basic for Applications (VBA) is an event-driven programming language 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 is now used for the entire Office suite and over 200 non-Office hosts.

Visual Basic for Applications (VBA) is an event-driven programming language first introduced by Microsoft in 1993 designed to give Excel 5.0 a more robust object-oriented language for writing macros and automating the use of Excel. The language and its runtime quickly matured and began being used in products beyond Microsoft Office applications.

The last full version, VBA 6, was shipped in 1998 and includes a myriad of licensed hosts, among them: Office 2000 - 2010, AutoCAD, PI Processbook, and the stand-alone Visual Basic 6.0. VBA 6 code will run equally well on any host, though the underlying objects native to each host will vary. Though still built into Microsoft Office applications, VBA 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 only been slightly updated since, only including new features to allow it to remain compatible with x64 versions of Windows and Office. 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 compabitility and 64-bit-safe issues.

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".

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, such as Variant. Below is a simple subroutine which generates a message box and prints a message to the Immediate window:

Public Sub HelloWorld()
Dim message As String
message = "Hello World"
MsgBox message
Debug.Print "I just made a message box that says """ & message & """!"
End Sub


Beginner Resources

General VBA Information and History

## "not responding" isn't broken code

If you're reviewing a VBA question, please don't downvote and/or vote to close VBA questions on the basis of "broken code" or "not working as intended" when the OP mentions the host application going unresponsive. Most VBA hosts run VBA code on the main/UI thread, so it's perfectly normal that a long-running VBA macro makes the host application's main window caption say "(not responding)". In that state, the VBA code is running, and the host application is no longer handling Windows messages; clicking on the main window causes the screen to "white-out", which an inexperienced VBA coder may easily misinterpret as "Excel freezing", when in reality Excel is just busy running the OP's code. Responsiveness resumes when the code completes.

If you're asking a VBA question, please avoid wording this unresponsive state in such a way that could be interpreted as a bug in your code: avoid terms like "freezes" or "crashes" in your post (crashing code is off-topic) - if Excel is just not reaponding and your code eventually completes, your question is perfectly on-topic and probably just needs performance tuning... And you're on the right site for this.

Code Language (used for syntax highlighting): lang-vb