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
1 answer

Outlook VBA How to loop through inbox and list from email email address if subject matches

I'm trying to use Outlook VBA to loop through the inbox and list the from email address if the subject matches a string. Got this so far from googling, but it's not working: Dim objNS As Outlook.NameSpace Set objNS = GetNamespace("MAPI") Set Items =…
Superdooperhero
  • 7,584
  • 19
  • 83
  • 138
11
votes
2 answers

Calling stored procedure using VBA

I am working in Access 2010 user front-end with a Microsoft SQL Server 2008 back-end. The tables in Access are all linked to the SQL server database. I have a stored procedure that inserts new values (supplied by the parameters) into a table. I…
VictoriaJay
  • 381
  • 3
  • 8
  • 22
11
votes
3 answers

Excel VBA Macro: User Defined Type Not Defined

I'm getting the above error when trying to execute this macros. I'm pretty new to Macros and coding in general so please forgive the ignorance. Sub DeleteEmptyRows() Dim oTable As Table, oRow As Row, _ TextInRow As Boolean, i As…
holdo1
  • 133
  • 1
  • 2
  • 6
11
votes
2 answers

Excel VBA: how to cast a generic control object into a ComboBox object?

I need to cast a generic VBA form control object into a ComboBox object so that I can add items to it. The generic object won't allow me to insert items into my existing lists Dim ctlCurrent As MSForms.Control For Each ctlCurrent In…
Marcos Buarque
  • 3,318
  • 8
  • 44
  • 46
11
votes
1 answer

Get logged on username in Excel VBA - not the account running Excel (using RunAs)

I have logged into my workstation with my normal domain credentials. We shall call this AccountA. I then use the "run as a different user" to launch Excel. We shall call this AccountB. I do this because the permissions needed to query some SQL…
user3508511
  • 153
  • 1
  • 2
  • 9
11
votes
5 answers

How to specify a different file path for a saved Excel import

So I have used doCmd.TransferText many times to use a saved text import specification, as you can easily saved the file path returned from an Application.FileDialog(msoFileDialogFilePicker) to find a select the file you wish to import with the saved…
pegicity
  • 359
  • 1
  • 3
  • 12
11
votes
5 answers

How to set recurring schedule for xlsm file using Windows Task Scheduler

I have a xlsx macro enabled file . How can I set it in the task manager so that everyday at 9 AM task manager would open the workbook, fire the macro and close the workbook. So far i am using Application.OnTime . . . But i realize that keeping the…
Andrei Vasilev
  • 597
  • 5
  • 18
  • 37
11
votes
2 answers

Word Automation: Write RTF text without going through clipboard

I am trying to replace the current selection in Word (2003/2007) by some RTF string stored in a variable. Here is the current code: Clipboard.SetText(strRTFString, TextDataFormat.Rtf) oWord.ActiveDocument.ActiveWindow.Selection.PasteAndFormat(0) Is…
Vincent
  • 22,366
  • 18
  • 58
  • 61
11
votes
3 answers

Using Application.Run in Excel VBA when workbook name contains spaces

Application.Run "MyWorkBook.xls!Macro1" will work (run the macro called Macro1 in the MyWorkBook.xls file). Application.Run "My Work Book.xls!Macro1" will not work (if the workbook name contains spaces, Excel says "the macro cannot be found"). I…
Laurent
  • 5,953
  • 14
  • 43
  • 59
11
votes
3 answers

Deleting columns from a table with merged cells

I'm trying to delete columns from a table which has horizontally merged cells Selection.MoveEnd Unit:=WdUnits.wdCell, Count:=3 Selection.Columns.Delete Eventhough columns are getting deleted, merged cells are removed in the process leaving a…
Raj
  • 4,405
  • 13
  • 59
  • 74
11
votes
2 answers

Using VBA to apply conditional formatting to a range of cells

I would like to know how to access the column in conditional formatting titled 'Applies To' and input my own conditions. I have included a screenshot for better reference. My code for adding the syntax in conditional formatting is, With Selection …
winhung
  • 553
  • 1
  • 5
  • 19
11
votes
2 answers

Getting AD Details based on username

I have a code to retrieve the details of a user from the AD such as email address, phone number etc, etc. The codes I am currently using is: Set objSysInfo = CreateObject("ADSystemInfo") strUser = objSysInfo.UserName msgbox(strUser) Set objUser =…
LuxuryWaffles
  • 1,518
  • 4
  • 27
  • 50
11
votes
1 answer

Programmatically control/intercept a Data Table refresh

Background I have an extremely large data table that takes up to 12 hours to run for around 1 million input scenarios on a high-end 64bit machine. The scenarios are based on a number of discrete Excel models, that are then fed into a financial model…
brettdj
  • 54,857
  • 16
  • 114
  • 177
11
votes
3 answers

How to determine if any row is selected in a listbox

I have a list box in my Access form. I need to know if any of the rows in this list box has been selected by the user. Is there any simple Access property or method exists for this purpose? I do not want to loop through the listbox to check if any…
got2nosth
  • 578
  • 2
  • 8
  • 27
11
votes
3 answers

Run time error 2185

I getting a run time error 2185, "You can't reference a property or method for a control unless the control has the focus ...". This is my code that I am using. Private Sub Command5_Click() Dim cardno As Integer cardno = cardnumber.Text …
Steve
  • 305
  • 2
  • 6
  • 16
1 2 3
99
100