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
55
votes
12 answers

vba: get unique values from array

Is there any built-in functionality in vba to get unique values from a one-dimensional array? What about just getting rid of duplicates? If not, then how would I get the unique values from an array?
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
55
votes
3 answers

Iterating through populated rows

So I am trying to iterate through a worksheet in an Excel spreadsheet using VBA. I want to iterate through each row, and then through each column, and despite googling, I can't actually find an intuitive way to do this. I'm assuming that the first…
user764357
55
votes
2 answers

VBA to copy a file from one directory to another

I have an access file that I regularly need to copy to another directory, replacing the last version. I would like to use an Excel macro to achieve this, and would also like to rename the file in the process. fileName =…
harryg
  • 23,311
  • 45
  • 125
  • 198
55
votes
12 answers

How to open a folder in Windows Explorer from VBA?

I want to click a button on my access form that opens a folder in Windows Explorer. Is there any way to do this in VBA?
VBwhatnow
  • 1,552
  • 2
  • 13
  • 24
54
votes
6 answers

Populating VBA dynamic arrays

The following code gives me error 9 "subscript out of range". I meant to declare a dynamic array so that the dimension changes as I add elements to it. Do I have to create a "spot" on the array before I store something in it like in JS? Sub…
sebastien leblanc
  • 675
  • 1
  • 12
  • 28
54
votes
23 answers

Excel's fullname property with OneDrive

If I want to use the open Workbook object to get the fullname of an Excel file after saving it, but that file has been synchronized to OneDrive, I get a "https" address instead of a local one, which other programs cannot interpret. How do I get the…
Virtuoso
  • 908
  • 1
  • 8
  • 14
53
votes
3 answers

Delete Sheets and avoid Excel asking the user to confirm, using custom messages instead

I have a button that triggers a chain of events. One of these events is to delete a sheet. Before the user deletes anything, I pop up my custom YES/NO message asking them to confirm the whole process. Then comes the sub event of deleting the sheet,…
BuZz
  • 16,318
  • 31
  • 86
  • 141
53
votes
2 answers

Returning a regex match in VBA (excel)

I am trying to write a function for excel 2010 that will take a cell of unstructured text, look for something called an sdi value and, if found, return that number. The sdi value will appear as sdi ####. What I want is to return sdi and the…
TheoRose
  • 533
  • 1
  • 5
  • 6
53
votes
6 answers

VBA collection: list of keys

After I add some values to the VBA collection, is there any way to retain the list of all keys? For example Dim coll as new Collection Dim str1, str2, str3 str1="first string" str2="second string" str3="third string" coll.add str1, "first…
Artur Iwan
  • 1,151
  • 2
  • 10
  • 17
53
votes
2 answers

Is VBA an OOP language, and does it support polymorphism?

I am actually working on my first VBA project. (come from C++) I would like to improve an existing VBA project used by a Microsoft Excel workbook by implementing classes and polymorphism. My problem is: 1 - I read a lot of articles/forums which…
Axel Borja
  • 3,718
  • 7
  • 36
  • 50
53
votes
9 answers

Should I use Call keyword in VB/VBA?

I use the Call keyword when calling subs in VB/VBA. I know it's optional, but is it better to use it or leave it off? I've always thought it was more explicit, but maybe it's just noise. Also, I read this on another forum: Using the Call keyword…
Fred Loyant
  • 531
  • 1
  • 4
  • 3
53
votes
5 answers

Skip to next iteration in loop vba

I am trying to create a simple conditional loop that will go to the next iteration if a condition is true. The code I have so far is: For i = 2 To 24 Level = Cells(i, 4) Return = Cells(i, 5) If Return = 0 And Level = 0 Then 'Go…
Käse
  • 545
  • 1
  • 4
  • 4
53
votes
8 answers

Easiest way to loop through a filtered list with VBA?

If I have an auto filter set up in Excel and I want to loop through all the visible data in one column with VBA code, what's the easiest way to do this? All the hidden rows that have been filtered away should not be included, so a plain Range from…
mattboy
  • 2,870
  • 5
  • 26
  • 40
52
votes
15 answers

How to add default signature in Outlook

I am writing a VBA script in Access that creates and auto-populates a few dozen emails. It's been smooth coding so far, but I'm new to Outlook. After creating the mailitem object, how do I add the default signature to the email? This would be the…
PowerUser
  • 11,583
  • 20
  • 64
  • 98
52
votes
7 answers

Does the VBA "And" operator evaluate the second argument when the first is false?

Function Foo(thiscell As Range) As Boolean Foo = thiscell.hasFormula And (InStr(1, UCase(Split(thiscell.formula, Chr(40))(0)), "bar") > 0) End Function This function exists to test for the presence of a certain substring (bar, in this case)…
James
  • 2,454
  • 1
  • 22
  • 22