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

Remove the scroll bar on a list box

I'm using a userForm in vba and I have a list box. I want to remove the horoz. scroll bar from the bottom. Is there a way to do this? I don't see any option in the properties box.
Mike
  • 2,293
  • 13
  • 42
  • 56
11
votes
5 answers

Get formatted values from a multi-cell range

Dim myText As String myText= Range("a3").Text Returns the formatted value in cell A3, but myText= Range("a3:c7").Text gives me an error. How do I get strings representing formatted values from a multi-cell range, while preserving the number…
Thunder
  • 10,366
  • 25
  • 84
  • 114
11
votes
4 answers

How to check from .net code whether "Trust access to the VBA project object model" is enabled or not for an Excel application?

How to check from .net code whether "Trust access to the VBA project object model" is enabled or not for an Excel application? Manually I can check it from Excel application- File>Options>Trust Center>Trust Center Settings>Macro Settings>Trust…
Kushal Waikar
  • 2,976
  • 5
  • 26
  • 31
11
votes
4 answers

Sub vs. Function without a return value

What is the sense of a subroutine (Sub)? Why not use a Function without a return value? Edit What I mean is, why does the keyword Sub exist? I can use Function without declaring a return value and have the same, no?
testo
  • 1,052
  • 2
  • 8
  • 24
11
votes
4 answers

VBA Excel 2-Dimensional Arrays

I was trying to find out how to declare a 2-Dimensional array but all of the examples I have found so far are declared with set integers. I'm trying to create a program that will utilize two 2-Dimensional arrays and then perform simple operations…
Jesse Smothermon
  • 1,041
  • 10
  • 25
  • 36
11
votes
2 answers

Range() vs Cells() vs Range.Cells()

I know that the Range() and Cells() properties are equivalent methods of accessing cells on a worksheet. However, when is it wise to use Range.Cells() in this combination? I came across an example where they used…
rajomato
  • 1,167
  • 2
  • 10
  • 25
11
votes
1 answer

Office 365 versus desktop Excel macros

Sorry if the title is very vague, I've been trying to work my way around this for a little while now, but I have to say that I know very little about Office 365 and its abouts. I've developed a series of macros in Excel 2010 that include input of…
Valeria Noble
  • 151
  • 1
  • 1
  • 8
11
votes
3 answers

What are possible suffixes after variable name in VBA?

As I've already figured out, there is at least six of them: !@#$%&. Here is snip: Dim A!, B@, C#, D$, E%, F& Debug.Print "A! - " & TypeName(A) Debug.Print "B@ - " & TypeName(B) Debug.Print "C# - " & TypeName(C) Debug.Print "D$ - " &…
HolisticElastic
  • 937
  • 8
  • 17
11
votes
3 answers

How to loop rows with Excel VBA macro?

I am new to VBA, but pretty good with PHP. That being said, I'm struggling with VBA loops... I have this sheet with 40 rows called "SH1": SH1 A B C D E 1 2 One 1.0a 12 2 7 Two 2.0b 34 3 13 Three 3.0c …
Jeff
  • 5,962
  • 16
  • 49
  • 81
11
votes
2 answers

Scraper throws errors instead of quitting the browser when everything is done

I've written a scraper to parse movie information from a torrent site. I used IE and queryselector. My code does parse everything. It throws errors instead of quitting the browser when everything is done. If I cancel the error box then I can see the…
SIM
  • 21,997
  • 5
  • 37
  • 109
11
votes
1 answer

Excel: require Power Query add-in load from VBA macro

The scenario is a Windows Server 2012 R2, 64 bit; Excel 2010, 32 bit. Many users, just a couple of them have administrative privileges. I installed Power Query from the built-in administrator. Without asking anything, the add-in got installed for…
Enrico
  • 325
  • 1
  • 2
  • 15
11
votes
2 answers

User Define Type Read Error

I am developing a system for a (my) small business. I have about 20 data files (Clients / suppliers / stores items / fixed assets / rentals / employees ...etc.) Each record of these files is defined using a Type statement and written or read using a…
ian Simmons
  • 111
  • 5
11
votes
4 answers

Access Filter VBA

I'm trying to use a filter in vba like this: Private Sub Form_Load() Me.Filter = "[Alvo] = " & AlvoAtual Me.FilterOn = True Me.Requery End Sub Where AlvoAtual is global variable, but nothin happens. When I change the AlvoAtual…
user569709
  • 113
  • 1
  • 1
  • 5
11
votes
3 answers

Excel VBA - Interpret "N/A" Values

I am traversing a spreadsheet which contains a column of prices, in the form of double types. I am trying to locate a missing value which is shown on the spreadsheet as "n/a", but it is not letting me interpret this as a string type. The cell…
Tom
  • 369
  • 2
  • 6
  • 16
11
votes
5 answers

How can I check for or cancel MULTIPLE pending application.ontime events in excel VBA?

I'm using the Application.Ontime event to pull a time field from a cell, and schedule a subroutine to run at that time. My Application.Ontime event runs on the Workbook_BeforeSave event. As such, if a user (changes the desired time + saves the…
goofology
  • 914
  • 2
  • 10
  • 21