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
85
votes
10 answers

Getting Error 3340 Query ' ' is corrupt while executing queries DoCmd.RunSQL

Since installing the windows update for Office 2010 resolving KB 4484127 I get an error while executing queries which contain a WHERE clause. For example executing this query: DoCmd.RunSQL "update users set uname= 'bob' where usercode=1" Results in…
Zvi Redler
  • 1,708
  • 1
  • 18
  • 29
85
votes
9 answers

Why Use Integer Instead of Long?

I often see questions relating to Overflow errors with vba. My question is why use the integer variable declaration instead of just defining all numerical variables (excluding double etc.) as long? Unless you're performing an operation like in a for…
Gareth
  • 5,140
  • 5
  • 42
  • 73
84
votes
10 answers

Open Excel file for reading with VBA without display

I want to search through existing Excel files with a macro, but I don't want to display those files when they're opened by the code. Is there a way to have them open "in the background", so to speak?
notnot
  • 4,472
  • 12
  • 46
  • 57
83
votes
14 answers

How can I create a progress bar in Excel VBA?

I'm doing an Excel app that needs a lot data updating from a database, so it takes time. I want to make a progress bar in a userform and it pops up when the data is updating. The bar I want is just a little blue bar moves right and left and repeats…
darkjh
  • 2,821
  • 7
  • 35
  • 43
83
votes
11 answers

Way to run Excel macros from command line or batch file?

I have an Excel VBA macro which I need to run when accessing the file from a batch file, but not every time I open it (hence not using the open file event). Is there a way to run the macro from the command line or batch file? I'm not familiar with…
Polymeron
  • 1,414
  • 1
  • 13
  • 12
82
votes
10 answers

How to merge rows in a column into one cell in excel?

E.g A1:I A2:am A3:a A4:boy I want to merge them all to a single cell "Iamaboy" This example shows 4 cells merge into 1 cell however I have many cells (more than 100), I can't type them one by one using A1 & A2 & A3 & A4 what can I do?
red23jordan
  • 2,841
  • 10
  • 41
  • 57
82
votes
3 answers

How can I concatenate strings in VBA?

This question comes from a comment under Range.Formula= in VBA throws a strange error. I wrote that program by trial-and-error so I naturally tried + to concatenate strings. But is & more correct than + for concatenating strings?
ilya n.
  • 18,398
  • 15
  • 71
  • 89
82
votes
3 answers

Excel: How to check if a cell is empty with VBA?

Via VBA how can I check if a cell is empty from another with specific information? For example: If A:A = "product special" And B:B is null Then C1 = "product special" Additionally, how can I use a For Each loop on theRange and how can I return…
Regis Santos
  • 3,469
  • 8
  • 43
  • 65
81
votes
7 answers

Detect whether Excel workbook is already open

In VBA, I opened an MS Excel file named "myWork.XL" programmatically. Now I would like a code that can tell me about its status - whether it is open or not. I.e. something like IsWorkBookOpened("myWork.XL) ?
user1222679
  • 923
  • 2
  • 9
  • 6
81
votes
16 answers

How can I URL encode a string in Excel VBA?

Is there a built-in way to URL encode a string in Excel VBA or do I need to hand roll this functionality?
Matthew Murdoch
  • 30,874
  • 30
  • 96
  • 127
81
votes
13 answers

What are some good patterns for VBA error handling?

What are some good patterns for error handling in VBA? In particular, what should I do in this situation: ... some code ... ... some code where an error might occur ... ... some code ... ... some other code where a different error might occur…
jwoolard
  • 6,024
  • 9
  • 37
  • 37
81
votes
21 answers

Use VBA to Clear Immediate Window?

Does anyone know how to clear the immediate window using VBA? While I can always clear it myself manually, I am curious if there is a way to do this programmatically.
Alpha
  • 2,372
  • 3
  • 21
  • 23
80
votes
8 answers

Is there a need to set Objects to Nothing

I always read that it is recommended to set objects to nothing, once I am done with them. But I normally use them only in functions inside forms. Isn't the reference lost and memory released when the function scope is left, regardless of setting…
Ramon
  • 1,021
  • 1
  • 7
  • 10
80
votes
9 answers

Refresh Excel VBA Function Results

How can I get a user-defined function to re-evaluate itself based on changed data in the spreadsheet? I tried F9 and Shift+F9. The only thing that seems to work is editing the cell with the function call and then pressing Enter.
Brian Sullivan
  • 27,513
  • 23
  • 77
  • 91
79
votes
1 answer

Convert cells(1,1) into "A1" and vice versa

I am working on an worksheet generator in Excel 2007. I have a certain layout I have to follow and I often have to format cells based on input. Since the generator is dynamic I have to calculate all kinds of ranges, merge cells, etc. How can I…
user366121
  • 3,203
  • 9
  • 48
  • 69