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

Loop over PDF files and transform them into doc with word

I am trying to use VBA coding - which I am pretty new to - to obtain a series of .doc documents from PDFs (which are not images), that is, I am trying to loop over various PDF files and save them in MS Word format. My experience is that word reads…
John Doe
  • 212
  • 1
  • 9
  • 28
11
votes
3 answers

Finding An LDAP URL?

I would like to access the company server's Active Directory so I can write a simple phonebook program. It would seem that I need to use LDAP to connect to it in order to grab a recordset. Is there anyway to figure out what the LDAP URL is based…
NaV
  • 703
  • 4
  • 9
  • 19
11
votes
1 answer

Excel: ClearContent preserving formulas

In Excel VBA is there a way to safely use Range("A1:B2").ClearContent without deleting the existent formulas in the cells? Just to make my scenario clearer: I'm pulling fresh data from the database and just want to erase everything on the sheet but…
Lorenzo
  • 4,558
  • 11
  • 44
  • 54
11
votes
6 answers

What will happen with Office VBA?

The company I work at is run on Excel sheets. Several of those sheets have some form of VBA code embedded. I'm doing some maintenance on them, but it feels really dated. What's going to happen to Office VBA? Why hasn't Microsoft released an embedded…
Andomar
  • 232,371
  • 49
  • 380
  • 404
11
votes
1 answer

Architecture of an Excel application

after 10 years of programming I find myself with the daunting task of creating my first Excel application in Excel 2007. I have programmed in VBA before on MS Access so this is not really a technical challenge for me but it's a real change of…
Lorenzo
  • 4,558
  • 11
  • 44
  • 54
11
votes
7 answers

Want VBA in excel to read very large CSV and create output file of a small subset of the CSV

I have a csv file of 1.2 million records of text. The alphanumeric fields are wrapped in quotation marks, the date/time or numeric fields are not. For example "Fred","Smith",01/07/1967,2,"7, The High Street","Anytown","Anycounty","LS1 7AA" What I…
C J
11
votes
3 answers

How to load and unload a Userform

Where should I put Load and Unload frm1 (Userform name is frm1) and where should I put Me.Show and Me.Hide? The (x) button within the UserForm doesn't work. My Load and Unload is within the Active-X command button code on Sheet1: Private Sub…
SAFD
  • 145
  • 1
  • 1
  • 9
11
votes
2 answers

How can I insert variable into formula in VBA

Can anyone solve this? Sub test Dim i as integer For I = 1 to 10 ActiveCell.Offset(0, 2).Formula = "=Sum(E15,&i&)" Next I End Sub
gonzalloe
  • 313
  • 3
  • 7
  • 22
11
votes
3 answers

Convert VBA String to Double

I am using the very basic coding of VBA for word to create a template that pulls data from other screens in windows. When it pulls the numbers, they are formatted as strings. I now need to get the strings to be converted to doubles, in order to add/…
c.olson
  • 111
  • 1
  • 1
  • 3
11
votes
5 answers

Split cell values into multiple rows and keep other data

I have values in column B separated by commas. I need to split them into new rows and keep the other data the same. I have a variable number of rows. I don't know how many values will be in the cells in Column B, so I need to loop over the array…
MJ95
  • 459
  • 1
  • 6
  • 22
11
votes
2 answers

Using Dictionary.Exists in a UserForm property is giving Error 404 object required

I am trying to use Class properties Get and Let in a UserForm called UBidStatus to fill a dictionary called DicOption. Everything works fine until the If Not DicOption(OptName).Exists line (Error 404 object required). NOTE: The code is working if I…
Gab
  • 135
  • 9
11
votes
3 answers

Excel UDF weighted RANDBETWEEN()

Well not really RANDBETWEEN(). I'm trying to create a UDF to return the index of a number within an array, where the larger the number the more likely it is to be chosen. I know how to assign probabilities to random numbers in a worksheet (i.e.…
Greedo
  • 4,967
  • 2
  • 30
  • 78
11
votes
3 answers

TRIM function/remove spaces from cells using VBA

I am using the code below to trim some "blank cells" that contain a space. The thing is it takes too much time, as is looping to every cell. What I want is to remove the spaces(the ones in the beginning and end, not middle), of all the cells. Is…
Leandro Moreira
  • 215
  • 4
  • 17
11
votes
1 answer

Copying sheets while preserving digital signatures

-- Edit: this is now part of the bigger question of how to reliably move sheets about in this question's context -- (Note: during preparing this post and testing solutions, I probably have already answered my own question. Just posting this in the…
Carl Colijn
  • 1,423
  • 9
  • 29
11
votes
3 answers

Using a centralized VBA module in multiple Access databases

I've been put in charge of 50+ Access databases (.mdb's and .accdb's of varied versions) spread around an intranet, and I need to add a few VBA functions (the exact same code) to almost all of them. I'm hoping it can be accomplished by creating a…
spudsy
  • 127
  • 1
  • 1
  • 5