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
3 answers

Determining a User's Group Membership

How can I determine if a user, in say Access, is a member of an Active Directory Security Group? I'd rather not build a whole authentication system into my little Access DB. Thanks
Allain Lalonde
  • 91,574
  • 70
  • 187
  • 238
11
votes
5 answers

Refer to sheet using codename

I get a "type mismatch" error in this code: With Worksheets(Sheet1) '* Error here 'my code here End With My sheet's CodeName is 'sheet1'. Can someone please help me remove the error?
user007
  • 159
  • 2
  • 2
  • 15
11
votes
1 answer

IntelliSense in custom COM classes in VBA

Is there a way to get IntelliSense in own built COM classes in VBA? E.g. in the example below I would like to get "Number" showing up, whenever I press on the dot (or ctrl+space for shortcut): I suppose, if this is somehow resolved, I would also…
Vityata
  • 42,633
  • 8
  • 55
  • 100
11
votes
3 answers

Delete worksheet if it exists and create a new one

I want to look through my Excel worksheets and find a sheet with a certain name and delete that sheet if it is found. Afterwards I want to create a sheet after all existing sheets with that name. My code is as follows: For Each ws In Worksheets …
BIGGEST
  • 209
  • 1
  • 3
  • 13
11
votes
2 answers

Why is VBA.Collection.Count a method

The VBA Collection has 5 members, all of which are methods: Add, Count, Item, _NewEnum and Remove. Of the 5 members, the Count method looks like it could/should be a getter rather than a method. Is this a legacy of OOP/MS design from the early days…
ThunderFrame
  • 9,352
  • 2
  • 29
  • 60
11
votes
2 answers

Exclude first row (it is row #1 in my sheet) from USEDRANGE method?

I have some input data from my broker. I've written some code to automate calculations, adding columns and inserting some formulas. In the end, I want to do some conditional formatting (affecting whole rows) to determine transactions that are…
michal roesler
  • 479
  • 2
  • 9
  • 26
11
votes
4 answers

Book list - getting book details from amazon using Excel VBA barcode lookups

I have a barcode reader and bunch of books. For each of the books, I want to list the book name and the author in an Excel spreadsheet. My view is that some VBA code connecting to an Amazon web service would make this easier. My questions is -…
hawkeye
  • 34,745
  • 30
  • 150
  • 304
11
votes
2 answers

ADO Recordset data not showing on form

I've got a frustrating issue on MS Access 2010 that I would at this stage qualify as a bug. And after having tried all possible workarounds, I am out of ideas and rely on you. Context Huge Ms Access 2010 application with 25k lines of VBA and >50…
Thomas G
  • 9,886
  • 7
  • 28
  • 41
11
votes
4 answers

Using Range.Replace on Formula Cells

I have a short list of values from A1 through A10: A4 contains the string ab and A5 contains the formula: ="a" & "b" If I run: Sub Test1() Dim r As Range Set r = Range("A1:A10") r.Replace What:="ab", Replacement:="x" End Sub only A4…
Gary's Student
  • 95,722
  • 10
  • 59
  • 99
11
votes
3 answers

Embed an R process in a VBA macro

the title exactly -- is there a way to call an r process from an excel macro?
Thomas
  • 847
  • 4
  • 12
  • 21
11
votes
2 answers

Why does this code compile when pasted in but fail otherwise?

A friend made me look at this page, and noticed a strange piece of code in the signature of one of the forum users. The code is a one-liner that goes as follows: On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To…
Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
11
votes
4 answers

C# Char from Int used as String - the real equivalent of VB Chr()

I am trying to find a clear answer to my question and it is not a duplicate of any other questions on the site. I have read many posts and related questions on this on SO and several other sites. For example this one which is the key answer (many…
ib11
  • 2,530
  • 3
  • 22
  • 55
11
votes
3 answers

How to end a do while loop in VBA

I would like to know what is wrong with my coding as I am unable to end my do while loop in Microsoft Excel VBA. I wish to end this do while loop if the next line is blank. Do While Cells(RowName, 1) <> "" Name = Cells(RowName, ColumnName) …
Linify
  • 227
  • 4
  • 13
11
votes
5 answers

Excel VBA - Return selected element in slicer

I have a slicer called 'Slicer_HeaderTitle'. I simply need to be able to dim a variable in VBA with the value of the selected element. I'll only have one element selected at a time. I've had a lot of problems with selecting and de-selecting…
TobiasKnudsen
  • 527
  • 2
  • 9
  • 29
11
votes
2 answers

Generic Way to Determine the Maximum Allowed Length a of String

Take a look at this property(Given you have a table on the first worksheet): Application.Sheets(1).ListObjects(1).name How many characters can this property contain? Well, after testing out a few strings I've come to the conclusion that its 255,…
David Rogers
  • 2,601
  • 4
  • 39
  • 84