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

Run a Powershell command (not script) from Excel VBA

I've searched SO, and I can find plenty of examples of running a PowerShell script from VBA, but I can't find any examples of just running a simple command. For example, this works: Dim retval As Variant retval = Shell("PowerShell…
Tim
  • 2,701
  • 3
  • 26
  • 47
11
votes
3 answers

Digital signing of VBA project under Windows 10

Yesterday I upgraded from win 8.1 to win 10. In Outlook 2013 under win 8.1 I had a VBA script (macro) which was automatically adding BCC to every mail I have sent. This script was digitally signed so that I can have the Macro setting like this:…
Mario
  • 377
  • 5
  • 18
11
votes
3 answers

Multiple Variable Arguments to Application.OnTime

I am working on a data acquisition frontend for Excel 2010. I can't figure out the syntax for passing multiple local variable arguments to Application.OnTime. http://markrowlinson.co.uk/articles.php?id=10 provides a good example for a single…
solemn
  • 119
  • 1
  • 1
  • 4
11
votes
1 answer

VBA Integer Overflow at 70,000

I have a worksheet with 70,000 rows of data and a simple macro that counts the total number of rows: Dim LastRow as Integer LastRow = SourceSheet.Cells.Find(what:="*", searchdirection:=xlPrevious, searchorder:=xlByRows).Row MsgBox…
DixieFlatline
  • 484
  • 2
  • 5
  • 17
11
votes
3 answers

How to create a MS-Word-Form-Region which allows above-insertion?

I want to create a Form in Microsoft Word which is quite dynamic and enables the user to add multiple regions. The relevant data contains date-specific events which are sorted descendant. If you insert a new event, it has to appear on top of the…
Simon K.
  • 348
  • 1
  • 7
  • 24
11
votes
5 answers

How do I call an Excel macro from Python using xlwings?

I've read the API docs for xlwings, and played around with Workbook and Sheet objects in the interpreter, but I can't figure out how to call a macro from Python. How do I use xlwings to call an Excel macro from Python?
ABM
  • 1,628
  • 2
  • 26
  • 42
11
votes
1 answer

Programmatically rebuild .exd-files when loading VBA

After updating Microsoft Office 2007 to Office 2010 some custom VBA scripts embedded in our software failed to compile with the following error message: Object library invalid or contains references to object definitions that could not be…
aspartame
  • 4,622
  • 7
  • 36
  • 39
11
votes
1 answer

Error, Unable to set the formulaArray property of the range class

I've created the following…
Phairplay
  • 273
  • 2
  • 4
  • 16
11
votes
2 answers

Save excel as pdf changing its orientation to horizontal

I have an excel sheet with too many columns, so when I save it as a PDF the file its in too many pages and its not easy to read . I need to save the sheet as pdf but before I want to change its orientation to horizontal so I can read the pdf with no…
diego
  • 169
  • 1
  • 1
  • 8
11
votes
6 answers

Excel - Combine multiple columns into one column

I have multiple lists that are in separate columns in excel. What I need to do is combine these columns of data into one big column. I do not care if there are duplicate entries, however I want it to skip row 1 of each column. Also what about if …
Akib
  • 111
  • 1
  • 1
  • 3
11
votes
4 answers

How to place text in the clipboard so that it pastes as a table in Word?

Using VBA in MS Office, how do I add text to the Windows clipboard so that it will paste into Word as a table?
Shane Miskin
  • 1,911
  • 2
  • 22
  • 30
11
votes
4 answers

How do I prevent a PivotChart from becoming a regular chart on sheet copy?

Using Excel 2010, I have written some VBA to copy selected sheets from a Master workbook to a Client workbook. The code works just fine to copy the data sheet which has data & PivotTable(s) associated with the data, and the chart sheet with one or…
FreeMan
  • 5,660
  • 1
  • 27
  • 53
11
votes
2 answers

Autofit Method of Range Class Failed (Run Time Error 1004)

This is just part of my code. the value from the textbox here already gets copied to the specific cell in the Bank Certification worksheet. I need to make sure that cell C5 is specifically fitted regardless of the length of the text i inputted in…
Benedict Solpico
  • 139
  • 1
  • 1
  • 10
11
votes
1 answer

Putting a hyperlink in a MessageBox

Is it possible to add a hyperlink to a messagebox? I'm trying to do something like this: MsgBox "Sorry, but you have an out-of-date database version. Please redownload via the batch file to ensure that you have the latest version. Contact the…
Johnny Bones
  • 8,786
  • 7
  • 52
  • 117
11
votes
5 answers

How do I open a file if I only know part of the file name?

I need to open a file whose full filename I do not know. I know the file name is something like. filename*esy I know definitely that there's only one occurrence of this file in the given directory.
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062