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
79
votes
7 answers

Excel column number from column name

How to get the column number from column name in Excel using Excel macro?
Paritosh Ahuja
  • 1,239
  • 2
  • 10
  • 19
78
votes
9 answers

Creating an Array from a Range in VBA

I'm having a seemingly basic problem but can't find any resources addressing it. Simply put, I just want to load the contents of a Range of cells (all one column) into an Array. I am able to accomplish this by means of DirArray = Array(Range("A1"),…
basaltanglia
  • 793
  • 1
  • 6
  • 12
78
votes
8 answers

How do I express "if value is not empty" in the VBA language?

How do I express the condition "if value is not empty" in the VBA language? Is it something like this? "if value is not empty then..." Edit/Delete Message
excel34
  • 789
  • 1
  • 6
  • 4
78
votes
1 answer

Retrieve calendar items (Outlook API, WebDAV) displaying strange behaviour

We are writing an MS Outlook plugin. To satisfy our business-logic, it should check all appointments between some dates. We are experiencing several problems with retrieving all items from calendars. We tried two options: Outlook API. We use the…
Bolick
  • 781
  • 5
  • 4
77
votes
16 answers

How to extract file name from path?

How do I extract the filename myfile.pdf from C:\Documents\myfile.pdf in VBA?
Johan
  • 18,814
  • 30
  • 70
  • 88
76
votes
1 answer

Call a Subroutine from a different Module in VBA

Is it possible to call a function from one Module to another? I have the following code: Sub MAIN() Call IDLE End Sub MAIN is located in Module1 IDLE is located in Module2 and defined as: Sub IDLE()
Nimrod
  • 2,343
  • 3
  • 17
  • 7
76
votes
8 answers

How to add a named sheet at the end of all Excel sheets?

I am trying to add an Excel sheet named "Temp" at the end of all existing sheets, but this code is not working: Private Sub CreateSheet() Dim ws As Worksheet ws.Name = "Tempo" Set ws = Sheets.Add(After:=Sheets(Sheets.Count)) End Sub Can…
Behseini
  • 6,066
  • 23
  • 78
  • 125
76
votes
4 answers

How to use workbook.saveas with automatic Overwrite

In this section of code, Excel ALWAYS prompts: "File already exists, do you want to overwrite?" Application.DisplayAlerts = False Set xls = CreateObject("Excel.Application") Set wb = xls.Workbooks.Add fullFilePath = importFolderPath & "\" &…
bob.mazzo
  • 5,183
  • 23
  • 80
  • 149
75
votes
1 answer

What is the difference between Dim, Global, Public, and Private as Modular Field Access Modifiers?

In VB6/VBA, you can declare module-level variables outside of a specific Sub or Function method. I've used Private and Public before inside modules and understand them like so: Public - visible to all code inside the module and all code outside…
Ben McCormack
  • 32,086
  • 48
  • 148
  • 223
74
votes
6 answers

Convert string to int if string is a number

I need to convert a string, obtained from excel, in VBA to an interger. To do so I'm using CInt() which works well. However there is a chance that the string could be something other than a number, in this case I need to set the integer to 0. …
Captastic
  • 1,036
  • 3
  • 10
  • 19
74
votes
6 answers

Is it possible to write Excel VBA Code in Visual Studio

Is there a way to write VBA Code in Visual Studio. If not is there any other alternatives?
Dblock247
  • 6,167
  • 10
  • 44
  • 66
74
votes
9 answers

How to get current working directory using vba?

I am using MS Excel 2010 and trying to get the current directory using the below code, path = ActiveWorkbook.Path But ActiveWorkbook.Path returns blank.
Ullan
  • 905
  • 4
  • 15
  • 28
73
votes
5 answers

How to change Format of a Cell to Text using VBA

I have a "duration" column in an Excel sheet. Its cell format always changes — I want convert the duration from minutes to seconds, but because of the cell formatting it always gives me different answers. I was thinking that before doing the…
Code Hungry
  • 3,930
  • 22
  • 67
  • 95
73
votes
9 answers

Continue For loop

I have the following code: For x = LBound(arr) To UBound(arr) sname = arr(x) If InStr(sname, "Configuration item") Then '**(here I want to go to next x in loop and not complete the code below)** End If '// other…
DevilWAH
  • 2,553
  • 13
  • 41
  • 57
73
votes
5 answers

How can I use a for each loop on an array?

I have an array of Strings: Dim sArray(4) as String I am going through each String in the array: for each element in sarray do_something(element) next element do_something takes a string as a parameter I am getting an error passing the element…
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062