Questions tagged [vba]

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 .
  • If your question is specifically about programming Excel, Access, Word, Outlook, PowerPoint or Project, also use the appropriate application tag: , , , , or .
  • While code written in VBA is typically called a 'macro', the tag should not be used for general VBA questions. Please see the 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 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:

Beginner Resources:

References:

Additional Reading:

General VBA Information and History:

211298 questions
11
votes
3 answers

Excel forms: identify unused code

I am updating an excel app written by someone else (of course :) I found lots of unused Sub CommandButtonXX_Click() subs, and I am not always sure if the button still exists. Is there a way (program, VBE interface, external tool) to do some cleanup…
iDevlop
  • 24,841
  • 11
  • 90
  • 149
11
votes
2 answers

Open Outlook mail Item using EntryID, StoreID, and / or PR_ENTRYID

NOTE: I'm using VBA and Office 2007. (I would use C#, but the project parameters don't allow this) I'm attempting to find some method in Outlook, or an API, that will allow me to open an Outlook mail item by providing either the Outlook EntryID or…
Kyland Holmes
  • 111
  • 1
  • 1
  • 3
11
votes
5 answers

How Add a COM-Exposed .NET Project to the VB6 (or VBA) References Dialog?

I have created a .NET assembly that is exposed to COM according to the exceptional article Build and Deploy a .NET COM Assembly by Phil Wilson. And everything works fine in the sense that the .NET assembly is properly registered for COM, and…
Mike Rosenblum
  • 12,027
  • 6
  • 48
  • 64
11
votes
7 answers

Could Anyone Show List of Button Face Id in Excel 2010

I would like to create costum menu button using VBA in my excel 2010 file using predefined excel button that use face id. In my case i would like to use "lock" and "refresh" icon, but doesn`t know the face id for that icon. could anyone show or…
indrap
  • 760
  • 2
  • 10
  • 18
11
votes
3 answers

Sending formatted Lotus Notes rich text email from Excel VBA

I have little Lotus Script or Notes/Domino knowledge but I have a procedure, copied from somewhere a long time ago, that allows me to email through Notes from VBA. I normally only use this for internal notifications where the formatting hasn't…
Lunatik
  • 3,838
  • 6
  • 37
  • 52
11
votes
1 answer

How do I call VBA code in an Excel spreadsheet from Java?

I have an Excel file with a large set of VBA code. There are 4 public subroutines that take no parameters that can be called by the user when the document is opened in Excel, these manipulate the data in the various sheets as needed. We have a…
Reivax
  • 135
  • 1
  • 1
  • 10
11
votes
2 answers

Download file with progress meter in VBA

I've found numerous examples for doing this in several languages, but none that are VBA specific. This question, How to download multiple files in VB6 with progress bar?, addresses three different approaches to do this in VB6. Use the ASyncRead…
mwolfe02
  • 23,787
  • 9
  • 91
  • 161
11
votes
1 answer

MS Access prepared statements

Is it possible to execute a prepared statement in MS Access on a local table in VBA like this: UPDATE part SET part_description=? WHERE part_id=? If so how is it done?
David
  • 2,101
  • 2
  • 32
  • 41
11
votes
4 answers

Plotly: How to embed a fully interactive Plotly figure in Excel?

I'm trying to embed an interactive plotly (or bokeh) plot into excel. To do this I've tried the following three things: embed a Microsoft Web Browser UserForm into excel, following: How do I embed a browser in an Excel VBA form? This works and…
dusio
  • 480
  • 5
  • 18
11
votes
0 answers

Insert OLE Object into MS Word Document and keep the underlying format WMF intact

I am trying to replicate the following method in C# Word interop (NetOffice) Selection.PasteSpecial Link:=True, DataType:=wdPasteMetafilePicture, _ Placement:=wdInLine, DisplayAsIcon:=False I am saying "replicate" since I don't want to actually…
Rand Random
  • 7,300
  • 10
  • 40
  • 88
11
votes
3 answers

Word VBA "Project is unviewable"

Does anyone know how to make a template viewable in Word 2007? I created a template, Experiments.dotm, added it using Add-Ins, but when I try to create a module within it, get an error message: "Project is unviewable"
Bruce
  • 153
  • 1
  • 1
  • 10
11
votes
5 answers

Code for looping through all excel files in a specified folder, and pulling data from specific cells

I have about 50 or so Excel workbooks that I need to pull data from. I need to take data from specific cells, specific worksheets and compile into one dataset (preferably into another excel workbook). I am looking for some VBA so that I can compile…
Justin
  • 4,461
  • 22
  • 87
  • 152
11
votes
0 answers

Why can't I `End` code while I'm subclassing without breaking everything?

Context I've written some code in VBA to subclass a userform so that ultimately I can intercept WM_TIMER messages being dispatched to it. I'm doing this instead of specifying a TIMERPROC, as it allows me to use VBA's own error handling and calling…
Greedo
  • 4,967
  • 2
  • 30
  • 78
11
votes
4 answers

Can a class extend the Collection object?

I'm trying to extend functionality of the VBA Collection object in a new class and make this class an inheritant of Collection, but the Implements Collection statement gives me the following error: Bad interface for Implements: method has…
Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
11
votes
3 answers

What is the best way to access a serial port from VBA?

What is the best way to access a serial port from VBA? I have a need for some of our sales reps to be able to send a simple string over the serial port from an action button in PowerPoint. I don't commonly use VBA, especially for anything like this.…
GEOCHET
  • 21,119
  • 15
  • 74
  • 98