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
61
votes
2 answers

Determining the full type of a variable

By the full type of a variable I mean the sort of information that you get in the immediate window: I would like to determine the type information dynamically using VBA. The function TypeName() doesn't do what I want since it returns the subtype of…
John Coleman
  • 51,337
  • 7
  • 54
  • 119
61
votes
4 answers

How to find if an array contains a string

Possible Duplicate: How to search for string in MS Access VBA array I am currently working on an Excel macro, and I could not find a way to do like if array.contains(mystring) I wrote the following, and it gives me the message "Invaild Qualifier"…
Nicola-V
  • 633
  • 1
  • 5
  • 6
60
votes
9 answers

What does the Excel range.Rows property really do?

OK, I am finishing up an add-on project for a legacy Excel-VBA application, and I have once again run up against the conundrum of the mysterious range.Rows (?) and worksheet.Rows properties. Does anyone know what these properties really do and…
RBarryYoung
  • 55,398
  • 14
  • 96
  • 137
59
votes
5 answers

Excel function to make SQL-like queries on worksheet data?

I have a largish table in an Excel worksheet: Column_1 | Column_2 | Column_3 ValueA ValueB ValueC .... What I need is a function that will take as input the range and an SQL-like query String and return a range of rows that match the…
Richard H
  • 38,037
  • 37
  • 111
  • 138
59
votes
14 answers

How can I generate GUIDs in Excel?

I have an excel file with one order on each row, and I want each order to have a unique identifier, so there will be a Unique ID column. Every time I fill a row, I want Excel to automatically populate the Unique ID column for me. I did some research…
abw333
  • 5,571
  • 12
  • 41
  • 48
59
votes
6 answers

Properly Handling Errors in VBA (Excel)

I've been working with VBA for quite a while now, but I'm still not so sure about Error Handling. A good article is the one of CPearson.com However I'm still wondering if the way I used to do ErrorHandling was/is completely wrong: Block 1 On…
skofgar
  • 1,607
  • 2
  • 19
  • 26
58
votes
2 answers

How can I get this 8 year old VBA 64-bit compiler bug fixed?

So here's the bug: In a 64-Bit VBA host (for example Access 365 64-bit or Excel 2016 64-bit) create a class module SomeClass: ' this needs to be here to trigger the bug: Private Sub Class_Terminate() End Sub and then some module Test: Function…
Nordic Mainframe
  • 28,058
  • 10
  • 66
  • 83
58
votes
4 answers

Loop through each cell in a range of cells when given a Range object

Let's say I have the following code: Sub TestRangeLoop() Dim rng As Range Set rng = Range("A1:A6") ''//Insert code to loop through rng here End Sub I want to be able to iterate through a collection of Range objects for each cell…
Ben McCormack
  • 32,086
  • 48
  • 148
  • 223
58
votes
3 answers

Programmatic Access To Visual Basic Project Is Not Trusted

I have two scheduled tasks on my computer. They both open Excel files and run a macro & are pretty similar in what they do. They both work on my computer. However I moved the scheduled tasks onto a colleague's computer. One worked the other didn't.…
mHelpMe
  • 6,336
  • 24
  • 75
  • 150
58
votes
11 answers

Create excel ranges using column numbers in vba?

How is it possible to create a range in vba using the column number, rather than letter?
intrigued_66
  • 16,082
  • 51
  • 118
  • 189
58
votes
14 answers

How to Remove Line Break in String

I want to Remove the Line Break from the string if my string Ends with Line Break. Sub linebreak(myString) If Len(myString) <> 0 Then If Right$(myString, 1) = vbCrLf Or Right$(myString, 1) = vbNewLine Then myString = Left$(myString,…
Code Hungry
  • 3,930
  • 22
  • 67
  • 95
57
votes
3 answers

Add newline to VBA or Visual Basic 6

I want to concatenate two strings with a linebreak between them. st = "Line 1" + newline + "Line2" How do I add a newline to VBA or Visual Basic 6?
Gerhard Powell
  • 5,965
  • 5
  • 48
  • 59
56
votes
5 answers

Excel Date Conversion from yyyymmdd to mm/dd/yyyy

I have been searching for about an hour on how to do this in Excel. I have an Excel file that was created from an old system and I am pulling information from a SQL Server Database, I will be inputting the information back into the SQL Server…
Malachi
  • 3,205
  • 4
  • 29
  • 46
55
votes
10 answers

How to "flatten" or "collapse" a 2D Excel table into 1D?

I have a two dimensional table with countries and years in Excel. eg. 1961 1962 1963 1964 USA a x g y France u e h a Germany o x …
emmby
  • 99,783
  • 65
  • 191
  • 249
55
votes
4 answers

VBA Check if variable is empty

I have an object and within it I want to check if some properties are set to False, like: If (Not objresult.EOF) Then 'Some code End if But sometimes, objresult.EOF is Empty; how can I check for this? IsEmpty function is for excel cells…
fessguid
  • 559
  • 1
  • 5
  • 3