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
182
votes
27 answers

Function to convert column number to letter?

Does anyone have an Excel VBA function which can return the column letter(s) from a number? For example, entering 100 should return CV.
intrigued_66
  • 16,082
  • 51
  • 118
  • 189
180
votes
5 answers

How to get the path of current worksheet in VBA?

I wrote a macro as an add-in, and I need to get the path of the current worksheet on which it is being executed. How do I do this? How do I get the file path (just the directory)?
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
180
votes
13 answers

Excel VBA App stops spontaneously with message "Code execution has been halted"

From what I can see on the web, this is a fairly common complaint, but answers seem to be rarer. The problem is this: We have a number of Excel VBA apps which work perfectly on a number of users' machines. However on one machine they stop on certain…
Phil Whittington
  • 2,144
  • 2
  • 16
  • 20
169
votes
7 answers

What does the keyword Set actually do in VBA?

Hopefully an easy question, but I'd quite like a technical answer to this! What's the difference between: i = 4 and Set i = 4 in VBA? I know that the latter will throw an error, but I don't fully understand why.
Jon Artus
  • 6,268
  • 11
  • 42
  • 41
166
votes
8 answers

Declare and Initialize String Array in VBA

This should work according to another stack overflow post but its not: Dim arrWsNames As String() = {"Value1", "Value2"} Can anyone let me know what is wrong?
Kairan
  • 5,342
  • 27
  • 65
  • 104
166
votes
6 answers

How can I send an HTTP POST request to a server from Excel using VBA?

What VBA code is required to perform an HTTP POST from an Excel spreadsheet?
Matthew Murdoch
  • 30,874
  • 30
  • 96
  • 127
158
votes
5 answers

How do I put double quotes in a string in vba?

I want to insert an if statement in a cell through vba which includes double quotes. Here is my code: Worksheets("Sheet1").Range("A1").Value = "=IF(Sheet1!B1=0,"",Sheet1!B1)" Due to double quotes I am having issues with inserting the string. How do…
user793468
  • 4,898
  • 23
  • 81
  • 126
158
votes
2 answers

When editing Microsoft Office VBA, how can I disable the popup "Compile error" messages?

When you're editing a Microsoft Office VBA macro or function, you will often move your cursor from a line that you haven't finished. For example, to go copy something you want to paste into that line. But, if that partial line isn't syntactically…
Iain S
  • 2,643
  • 3
  • 23
  • 23
156
votes
9 answers

How do I declare a global variable in VBA?

I wrote the following code: Function find_results_idle() Public iRaw As Integer Public iColumn As Integer iRaw = 1 iColumn = 1 And I get the error message: "invalid attribute in Sub or Function" Do you know what I did wrong? I…
Nimrod
  • 2,343
  • 3
  • 17
  • 7
153
votes
24 answers

Test or check if sheet exists

Dim wkbkdestination As Workbook Dim destsheet As Worksheet For Each ThisWorkSheet In wkbkorigin.Worksheets 'this throws subscript out of range if there is not a sheet in the destination 'workbook that has the same name as the current…
yse
  • 1,541
  • 2
  • 10
  • 4
141
votes
5 answers

How to create and write to a txt file using VBA

I have a file which is manually added or modified based on the inputs. Since most of the contents are repetitive in that file, only the hex values are changing, I want to make it a tool generated file. I want to write the c codes which are going to…
danny
  • 1,587
  • 2
  • 12
  • 12
135
votes
9 answers

Deleting a file in VBA

Using VBA, how can I: test whether a file exists, and if so, delete it?
inglesp
  • 3,299
  • 9
  • 32
  • 30
133
votes
4 answers

Display milliseconds in Excel

I am trying to display milliseconds in an Excel macro. I have a column of integers which are timestamps in milliseconds (e.g. 28095200 is 7:48:15.200 am), and I want to make a new column next to it which keeps a running average and displays the…
Evelyn
  • 2,588
  • 3
  • 22
  • 47
132
votes
4 answers

Loop through each row of a range in Excel

This is one of those things that I'm sure there's a built-in function for (and I may well have been told it in the past), but I'm scratching my head to remember it. How do I loop through each row of a multi-column range using Excel VBA? All the…
Margaret
  • 5,749
  • 20
  • 56
  • 72
131
votes
10 answers

Parsing an ISO8601 date/time (including TimeZone) in Excel

I need to parse an ISO8601 date/time format with an included timezone (from an external source) in Excel/VBA, to a normal Excel Date. As far as I can tell, Excel XP (which is what we're using) doesn't have a routine for that built-in, so I guess I'm…
rix0rrr
  • 9,856
  • 5
  • 45
  • 48