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

Excel Crashing Sometimes When Closed from Shared Network Location

This problem will sound broad and non-specific, but I've tried many things and don't know where else to turn. I have an Excel VBA project - it has hundreds of lines of code and several modules so I can't really post it all. It works as a template -…
arbitel
  • 321
  • 6
  • 22
11
votes
1 answer

Is scoping broken in VBA?

Say you have this code in a module called Module1: Option Explicit Private Type TSomething Foo As Integer Bar As Integer End Type Public Something As TSomething In equivalent C# code if you made the Something field public, the code would…
Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
11
votes
4 answers

When using Sheets.Add from a UserForm the new sheet is "connected" to another sheet?

I'm having a problem when adding new sheets to a Workbook from a UserForm. Basically whenever I add a new sheet, it appears to be linked to another sheet in the same workbook. I have been able to reproduce this issue fairly easily by creating a new…
lisovaccaro
  • 32,502
  • 98
  • 258
  • 410
11
votes
6 answers

Calling python script from excel/vba

I have a python code that reads 3 arguments (scalars) and a text files and then returns me a vector of double. I want to write a macro in vba to call this python code and write the results in one of the same excel sheet. I wanted to know what was…
Anninha
  • 171
  • 2
  • 2
  • 6
11
votes
1 answer

Difference between Thisworkbook.name and Activeworkbook.name in VBA

Is there any difference between Thisworkbook and ActiveWorkbook. Example code : Sub workbook_name() MsgBox Thisworkbook.name End Sub Sub active_name() MsgBox Activeworkbook.name End Sub Both will return the same output, Is…
hackwithharsha
  • 901
  • 1
  • 16
  • 39
11
votes
4 answers

How do I delete all buttons on a worksheet?

I've got a bit of code that creates a Save button on a worksheet (held in the wsReport variable), but it doesn't remove previous buttons. Over time, they tend to build up. Is there any way to do something like this? wsReport.Buttons.All.Delete (Not…
Andrew Perry
  • 743
  • 2
  • 11
  • 32
11
votes
1 answer

Are there configuration settings to remember docked toolwindow positions in the VBE?

I've just installed the Rubberduck add-in. If I configure my vbe windows so I can see your duck-windows it all looks lovely but when I restart Excel it is back the way it was before: is there a way around this so my configuration of the vbe…
whytheq
  • 34,466
  • 65
  • 172
  • 267
11
votes
1 answer

Execute batch script once for multiple emails

I have multiple emails coming in (Each day I get 3 emails for Orders for 3 Categories). The emails subject are in the format: "ORDERS EXTRACT - [Category] - [Date]". Where [Category] can be Category 1, Category 2 or Category 3. [Date] is the date…
Tikkaty
  • 772
  • 1
  • 8
  • 24
11
votes
2 answers

How do I open workbook programmatically as read-only?

This is how I can open an excel file in vbA: Workbooks.Open(file-path) is there a way to specify that it should be open as read-only? The files I am opening have a password on them, and I always get the dialog that it can only be open as read only.
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
11
votes
7 answers

Search as you type with MS Access Combobox

I would like to create a simple search-as-you-type combobox in Microsoft Access as shown in the image below. NB: the above image is from a complicated implementation of what I am trying to achieve from here My combobox is named ctlSearch. Using…
iPwnTech
  • 545
  • 2
  • 5
  • 20
11
votes
1 answer

VBA: What is causing this string argument passed to ParamArray to get changed to a number (that looks suspiciously like a pointer)?

FINAL EDIT: It does indeed appear to be a compiler bug - see the accepted answer. Using VBA within Excel 2007, I have the following code in 'Class1': Option Explicit Public Function strange(dummy As String, ParamArray pa()) Debug.Print…
jtolle
  • 7,023
  • 2
  • 28
  • 50
11
votes
2 answers

How to loop through all controls in a form, including controls in a subform - Access 2007

As the title of my question suggest, how is it possible to loop through all the controls in a form, including subforms. For example I use the below sub routine to set the background colour of controls with the tag * Public Sub colCtrlReq(frm As…
noelmcg
  • 1,057
  • 3
  • 21
  • 44
11
votes
10 answers

How to clearcontents of a merged cells

I am trying to clear contents from cells but some of them are merged so I am getting the error 1004 :"We cant do that to merged cells" For l = 4 To 9 If ws.Cells(j, l).Interior.ColorIndex = 19 Then ws.Range(j, l).ClearContents …
Stupid_Intern
  • 3,382
  • 8
  • 37
  • 74
11
votes
1 answer

Using Sublime Text or Notepad++ as Editor and Builder for VBA

OK, so I have to learn VBA in my new line of work as we use Excel to more or less anything. The built-in VBA editor in Excel drives me absolutely bonkers - it feels like it's not made for writing code in an efficient way. I have been using Sublime…
Holene
  • 563
  • 1
  • 7
  • 26
11
votes
1 answer

Occasional VBA Method 'Activate' of object 'ChartData' failed

Before I begin, here is some history: Created VBA in Excel to open and read three (3) Excel files (includes itself) and input data into charts/tables/graphs into a PowerPoint presentation. This version runs beautifully. VBA kicked off by a User…
eggWorx
  • 111
  • 1
  • 5