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
6 answers

Excel keep loading my spreadsheet with an error

I keep getting this error when I load my spreadsheet. It makes me repair it, which strips all the validators. The file is saved as an xlsm. "Excel found unreadable content in '' Do you wnat to recover the contents of this workbook. If you trust the…
James
  • 1,764
  • 5
  • 31
  • 49
11
votes
5 answers

How can you run an Excel macro through a schedule task

I need to run a macro in an Excel spreadsheet through a schedule task. I've defined the job as below: "C:\Program Files\Microsoft Office\Office10\EXCEL.EXE" "" The problem is that when the job starts Excel displays a pop-up and asks if we want to…
Bsquare
  • 113
  • 1
  • 1
  • 6
11
votes
7 answers

Excel VBA Run-time error '13' Type mismatch

I created a macro for a file and first it was working fine, but today I've been opening and restarting the file and macro hundreds of times and I'm always getting the following error: Excel VBA Run-time error '13' Type mismatch I didn't change…
Diogo
  • 151
  • 1
  • 1
  • 5
11
votes
2 answers

Is it good practice to leave "Debug.Print" instructions in code that goes in "production"?

In Excel VBA, is it good practice to leave Debug.Print instructions in code that goes in "production" ? That is quite useful to debug the sheets realtime at the user's machine when something goes wrong. Does it affect performance when Visual Studio…
BuZz
  • 16,318
  • 31
  • 86
  • 141
11
votes
3 answers

Using visual basic to access subfolder in Inbox?

Dim olApp As Outlook.Application Dim objNS As Outlook.NameSpace Set olApp = Outlook.Application Set objNS = olApp.GetNamespace("MAPI") Set myOlItems = objNS.GetDefaultFolder(olFolderInbox).Items I have used the code above to access the main outlook…
kinkajou
  • 3,664
  • 25
  • 75
  • 128
11
votes
2 answers

Loop through cells and add to a range

How would I loop through cells B1 to J1 and add them to a range if they meet a certain criteria. For example. Dim Range1 As Range For i = 1 to 9 If Range("A1").Offset(1,i) meets a certain criteria Then **Add that cell to Range1** End If Next i I'm…
user1067701
  • 157
  • 1
  • 2
  • 7
11
votes
7 answers

How to retrieve this computer's IP address?

What is the least cumbersome (module-inclusion, code lenght, etc) way to retrieve the machine IP address (of the first interface open)? I know of some solutions using MSINET, but I believe we can do better. Don't reply Function HomeIP() as…
jpinto3912
  • 1,457
  • 2
  • 12
  • 19
11
votes
3 answers

Excel Add A Field To An Xml Map

I've a XML map on an Excel 2010 worksheet, that I refresh from the contents of an XML file on a regular basis. I recently added a new field to the XML data and wish this to show up on my XML map. I simply want to be able to add the new field…
Jason
  • 607
  • 3
  • 9
  • 25
11
votes
1 answer

Using VBA to read new Outlook Email?

I have the following code which tells when new message has arrived! Private Sub Application_NewMail() MsgBox "New Mail Has Arrived" End Sub How do I read the body,subject of this mail? Are there any good tutorials for outlook programming? I…
kinkajou
  • 3,664
  • 25
  • 75
  • 128
11
votes
7 answers

When is a MailItem not a MailItem?

I have written a message handler function in Outlook's Visual Basic (we're using Outlook 2003 and Exchange Server) to help me sort out incoming email. It is working for me, except sometimes the rule fails and Outlook deactivates it. Then I turn…
Meomaxy
11
votes
3 answers

VBA project explorer still showing the excel workbooks which are closed by the macro

I have created a macro that opens a excel workbook and performs some operations and then at the end it closes the file.The macro is running fine. Now, when I open my VBA Project explorer in the vba editor. It shows the list of files that have been…
niko
  • 9,285
  • 27
  • 84
  • 131
11
votes
3 answers

Is there any way I can speed up this VBA algorithm?

I am looking to implement a VBA trie-building algorithm that is able to process a substantial English lexicon (~50,000 words) in a relatively short amount of time (less than 15-20 seconds). Since I am a C++ programmer by practice (and this is my…
GRB
  • 3,982
  • 4
  • 27
  • 21
11
votes
1 answer

Getting Combobox.Value in Access VBA

I have this code: If Me.Combobox.Value = "My Text" Then As I want to test if "My Text" is the only value selected, however, the conditional is skipped. I have also tried Me.Combobox.Column(1) and Me.Combobox.Text I believe there is some simple…
hrezs
  • 782
  • 1
  • 8
  • 23
11
votes
1 answer

vba positive lookahead is too greedy

I'm using Access VBA to parse a string with regex. Here's my regex function: Function regexSearch(pattern As String, source As String) As String Dim re As RegExp Dim matches As MatchCollection Dim match As match Set re = New RegExp re.IgnoreCase…
sigil
  • 9,370
  • 40
  • 119
  • 199
11
votes
3 answers

Setting the Item property of a Collection in VBA

I'm surprised at how hard this has been to do but I imagine it's a quick fix so I will ask here (searched google and documentation but neither helped). I have some code that adds items to a collection using keys. When I come across a key that…
user567677