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
65
votes
5 answers

What are .NumberFormat Options In Excel VBA?

Can you please let me know what are the .NumberFormat format options in Excel VBA? As you are fully aware Excel 2010 supports the following types: I know that we can set for example Text type as: .NumberFormat ="@" or for number: .NumberFormat =…
user1760110
  • 2,256
  • 5
  • 29
  • 37
64
votes
3 answers

How to activate a specific worksheet in Excel?

I just need to activate a certain worksheet. I have a string variable that keeps the name of the worksheet.
Alex
  • 649
  • 1
  • 5
  • 3
64
votes
13 answers

Rounding a number to the nearest 5 or 10 or X

Given numbers like 499, 73433, 2348 what VBA can I use to round to the nearest 5 or 10? or an arbitrary number? By 5: 499 -> 500 2348 -> 2350 7343 -> 7345 By 10: 499 -> 500 2348 -> 2350 7343 -> 7340 etc.
matt wilkie
  • 17,268
  • 24
  • 80
  • 115
64
votes
5 answers

How to name an object within a PowerPoint slide?

So I know how to name a textbox, or a like object in PowerPoint with VB, but I was wondering if there was a way to name objects through the Ribbon (PowerPoint 2007). For instance, if I add a text box onto a slide, is there a way to assign it a name…
Justin
  • 4,461
  • 22
  • 87
  • 152
64
votes
4 answers

How to continue the code on the next line in VBA

I would like to type the mathematical forumla in VBA code which many lines. I would like to split it into many lines. How do I do it? For example: U_matrix(i, j, n + 1) = k * b_xyt(xi, yi, tn) / (4 * hx * hy) * U_matrix(i + 1, j + 1, n) + (k *…
lakshmen
  • 28,346
  • 66
  • 178
  • 276
63
votes
15 answers

How to wait until ActiveWorkbook.RefreshAll finishes before executing more code

I have a subroutine that calls ActiveWorkbook.RefreshAll to bring new data in from an XML source on a website, and then performs multiple modifications to it. The problem is that not enough time is given for the RefreshAll command to finish, so the…
Mo2
  • 1,090
  • 4
  • 13
  • 26
63
votes
22 answers

How to check for empty array in vba macro

I want to check for empty arrays. Google gave me varied solutions but nothing worked. Maybe I am not applying them correctly. Function GetBoiler(ByVal sFile As String) As String 'Email Signature Dim fso As Object Dim ts As Object Set fso…
Vicky
  • 11,077
  • 11
  • 35
  • 29
63
votes
5 answers

Read/Parse text file line by line in VBA

I'm trying to parse a text document using VBA and return the path given in the text file. For example, the text file would look like: *Blah blah instructions *Blah blah instructions on line…
dancran
  • 721
  • 2
  • 9
  • 12
63
votes
1 answer

Use of symbol # (hash) in VBA Macro

What is the meaning of the use of the # symbol in Excel VBA? It is used like this: a = b /100# I don't understand the significance of # after the 100?
srinivasan
  • 823
  • 1
  • 8
  • 14
62
votes
1 answer

Excel cell from which a Function is called

How can I get the cell where my VBA Function is called from? In other words, what is the VBA equivalent for INDIRECT(ROW(), COLUMN()) ? I'm not looking for ActiveCell. What I want to do is have a simple function ThisRow_Col(rColumn As String) return…
Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
62
votes
2 answers

VBA shorthand for x=x+1?

Sub btn1_Click() Static value As Integer value = value + 1 MsgBox value End Sub I swear when I was taking a VB.net course in college there was a shorter way to tell a variable to add '' to itself. Maybe x=+1. I am using Access now though instead of…
gregg
  • 1,084
  • 1
  • 12
  • 25
62
votes
7 answers

How can I color dots in a xy scatterplot according to column value?

Consider the following worksheet: A B C D 1 COMPANY XVALUE YVALUE GROUP 2 Apple 45 35 red 3 Xerox 45 38 red 4 KMart 63 50 orange 5 Exxon 53 59 green I…
Pr0no
  • 3,910
  • 21
  • 74
  • 121
61
votes
7 answers

Return Index of an Element in an Array Excel VBA

I have an array prLst that is a list of integers. The integers are not sorted, because their position in the array represents a particular column on a spreadsheet. I want to know how I find a particular integer in the array, and return its index. …
H3lue
  • 1,081
  • 5
  • 16
  • 24
61
votes
2 answers

How to add a button programmatically in VBA next to some sheet cell data?

I have a function that generates data for say 100 rows (and 2 columns). For each row (in the 3rd column) I need to add a button which, when clicked, brings up a custom modal dialog box giving the user 4 options/buttons to choose from. Any idea how…
tobefound
  • 1,091
  • 2
  • 9
  • 15
61
votes
4 answers

Levenshtein Distance in VBA

I have excel sheet with data which I want to get Levenshtein Distance between them. I already tried to export as text, read in from script (php), run Levenshtein (calculate Levenshtein Distance), save it to excel again. But I am looking for a way to…
Yousf
  • 3,957
  • 3
  • 27
  • 37