VBA Reference Library

200 VBA object-model reference pages covering methods, properties, and events across Excel, Access, Word, and Outlook. Content is rebuilt from the official object-model documentation.

Access

50 reference pages

Add "(All)" to a combo box or list box

When you use a list box or combo box to enter selection criteria, you might want…

Add a record to a DAO Recordset

You can add a new record to a table-type or dynaset-type Recordset object by using the…

Allow Multiple Values Property

Applies to: Access 2013 | Access 2016 Specify whether the lookup column employs a multivalued field…

Allow users to add items to an unbound combo box

The following example uses the NotInList event to add an item to a combo box. To…

AllowBypassKey property

Applies to: Access 2013 | Access 2016 Use the AllowBypassKey property to specify whether or not…

AllowSpecialKeys property

Applies to: Access 2013 | Access 2016 Use the AllowSpecialKeys property to specify whether or not…

AllowZeroLength property

Applies to: Access 2013 | Access 2016 Use the AllowZeroLength property to specify whether a zero-length…

Assign a control a value from a table

Use the DLookup function to display the value of a field that is not in the…

Avg function (Microsoft Access SQL)

Applies to: Access 2013 | Access 2016 Calculates the arithmetic mean of a set of values…

Between…And operator

Applies to: Access 2013 | Access 2016 Determines whether the value of an expression falls within…

Bind a form to an ADO Recordset

To bind an Access form to a recordset, you must set the form's Recordset property to…

Calculate fields in domain aggregate functions

Use the string expression argument (the _expr_ argument) in a domain aggregate function to perform a…

Call procedures in a subform or subreport

You can call a procedure in a module associated with a subform or subreport in one…

Cancel an event

Under some circumstances, you may want to include code in an event procedure that cancels the…

Change tables involved in a one-to-many relationship in a DAO Recordset

Dynaset-type Recordset objects can be based on a multiple-table query containing tables with a one-to-many relationship.…

Count function (Microsoft Access SQL)

Applies to: Access 2013 | Access 2016 Calculates the number of records returned by a query.…

Count the number of records in a DAO Recordset

You may want to know the number of records in a Recordset object. For example, you…

Create a DAO Recordset from a form

You can create a Recordset object based on an Access form. To do so, use the…

Create a DAO Recordset from a query

You can create a Recordset object based on a stored select query. In the following code…

Create a DAO Recordset from a table in the current database

The following code example uses the OpenRecordset method to create a table-type Recordset object for a…

Create a user-defined property

The following example attempts to set the value of a user-defined property. If the property does…

Create an ADO connection string

To connect to a data source, you must specify a connection string, the parameters of which…

Date and time criteria expressions

To specify date or time criteria for an operation, you supply a date or time value…

Date and time criteria from a control on a form

If you want to change the _criteria_ argument for an operation based on a user's decision,…

Delete a record from a DAO Recordset

You can delete an existing record in a table or dynaset-type Recordset object by using the…

Detect the limits of a DAO Recordset

In a Recordset object, if you try to move beyond the beginning or ending record, a…

Extract data from a record in a DAO Recordset

After you have located a particular record or records, you may want to extract data to…

Find a record in a dynaset-type or snapshot-type DAO Recordset

Use the Find methods to locate a record in a dynaset-type or snapshot-type Recordset object. DAO…

Find a record in a table-type DAO Recordset

You use the Seek method to locate a record in a table-type Recordset object. When you…

Find the current position in a DAO Recordset

In some situations, you need to determine how far through a Recordset object you have moved…

Functions (alphabetical list)

The following topics describe common functions used in Access expressions. Common Access expression functions Abs functionArray…

Functions (category list)

The following list contains topics that describe common functions used in Access expressions. ActiveX CreateObject functionGetObject…

Hide a subform if the main form contains no records

The following example illustrates how to hide a subform named _Orders_Subform_ if its main form does…

Macro actions and methods of the DoCmd object

To carry out macro actions from code in Access, use the DoCmd object and its methods.…

Make bulk changes to a DAO Recordset

After you have created a table-type or dynaset-type Recordset object, you can change, delete, or add…

Min, Max functions (Microsoft Access SQL)

Applies to: Access 2013 | Access 2016 Return the minimum or maximum of a set of…

Multiple fields in criteria expressions

You can specify multiple fields in a _criteria_ argument. To specify multiple fields in the _criteria_…

Numeric criteria expressions

To specify numeric criteria for an operation, you supply a numeric value as part of the…

Numeric criteria from a control on a form

If you want to change the _criteria_ argument for an operation based on a user's decision,…

Prevent the accidental erasure of data when moving between controls on a form

When you tab from one text box or memo field to another in a form, the…

Quotation marks in string expressions

In situations where you must construct strings to be concatenated, you may need to embed a…

Refer to tab control objects in VBA

Use a tab control to present several pages of information about a single form. A tab…

Restrict data to a subset of records

When working with records, you'll often need to restrict your data to a specific set of…

Set properties of ActiveX Data Objects in Visual Basic

ActiveX Data Objects (ADO) enable you to manipulate the structure of your database and the data…

Sort records in case-sensitive (ASCII) order

Access sorts records in ascending or descending order without regard to case. However, you can use…

StDev, StDevP functions (Microsoft Access SQL)

Applies to: Access 2013 | Access 2016 Return estimates of the standard deviation for a population…

Sum function (Microsoft Access SQL)

Applies to: Access 2013 | Access 2016 Returns the sum of a set of values contained…

Textual criteria expressions

To specify textual criteria for an operation, you supply a text string as part of the…

Textual criteria from a control on a form

If you want to change the _criteria_ argument for an operation based on a user's decision,…

Var, VarP functions (Microsoft Access SQL)

Applies to: Access 2013 | Access 2016 Return estimates of the variance for a population or…

Excel

90 reference pages

ActiveX Controls

For more information about a specific control, select an object from the following list. CheckBox |…

Add a Table of Contents to a Workbook

The following examples show different approaches for adding a table of contents to an Excel workbook.…

Add a Unique List of Values to a Combo Box

These examples show different approaches for taking a list from a spreadsheet and using it to…

Add Controls to a Document

To add controls to a document, open the Control Toolbox, click the control you want to…

Add Controls to a User Form

To add controls to a user form, find the control you want to add in the…

Animate a Sparkline

You can animate a sparkline by using the ModifySourceData method of the SparklineGroup object to iterate…

Automatically Dismiss a Message Box

This example shows how to automatically dismiss a message box after a specified period of time.…

Built-In Dialog Box Argument Lists

|Dialog box constant|Argument list(s)| |:-----|:-----| |xlDialogActivate|window_text, pane_num| |xlDialogActiveCellFont|font, font_style, size, strikethrough, superscript, subscript, outline, shadow, underline,…

Cell Error Values

You can insert a cell error value into a cell or test the value of a…

Cell Formulas

Range.Formula and Range.Formula2 are two different ways of representing the logic in the formula. They can…

Cells and Ranges

This topic links to programming tasks (how-to and walkthrough topics) for common cells and ranges scenarios.…

Change the Color of the Horizontal Axis of a Sparkline

You can change the color of the horizontal axis of a sparkline by using the Color…

Chart object events

Chart events occur when the user activates or changes a chart. Events on chart sheets are…

Concepts (Excel VBA reference)

This section provides important concepts for developing custom Excel solutions. To navigate through the topics, use…

Control and dialog box events

After you have added controls to your dialog box or document, you add event procedures to…

Controlling One Microsoft Office Application from Another

If you want to run code in one Microsoft Office application that works with the objects…

Controls, Dialog Boxes, and Forms

This topic links to programming tasks (how-to and walkthrough topics) for common controls, dialog boxes, and…

Create a Custom Dialog Box

Use the following procedure to create a custom dialog box: 1. Create a User Form On…

Create a Custom Menu That Calls a Macro

The following code example shows how to create a custom menu with four menu options, each…

Create a Scheduling Calendar Workbook

The following code example shows how to use information in one workbook to create a scheduling…

Create a User Form

To create a custom dialog box, you must create a UserForm. To create a UserForm, click…

Create a Workbook

To create a workbook in Visual Basic, use the Add method. The following procedure creates a…

Create Address Labels from a Spreadsheet

Sample code provided by: Bill Jelen, MrExcel.com The following code example takes a list of addresses…

Create an HTML File with a Table of Contents based on Cell Data

This code example shows how to take data from a worksheet and create a table of…

Create or Replace a Worksheet

The following examples show how to determine if a worksheet exists, and then how to create…

Delete Duplicate Entries in a Range

The following example shows how to take a range of data in column A and delete…

Displaying a Custom Dialog Box

To test your dialog box in the Visual Basic Editor, click Run Sub/UserForm on the Run…

Events, Worksheet Functions, and Shapes

In this section Chart Object Events Control and Dialog Box Events QueryTable Object Events Worksheet Object…

Excel performance - Improving calculation performance

Applies to: Excel | Excel 2013 | Excel 2016 | VBA The "Big Grid" of 1…

Excel performance - Performance and limit improvements

Applies to: Excel | Excel M365| Excel 2016 | Excel 2013 | Excel 2010 | Office…

Excel performance - Tips for optimizing performance obstructions

Applies to: Excel | Excel 2013 | Office 2016 | VBA Follow these tips for optimizing…

Exporting a Range to a Table in a Word Document

This example takes the range A1:A10 on Sheet 1 and exports it to the first table…

Fill a Value Down into Blank Cells in a Column

The following example looks at column A, and if there is a blank cell, sets the…

Find All the Sparklines on a Sheet

The following code example uses a list box on a user form to display all of…

Formatting and VBA codes for headers and footers

The following special formatting and Visual Basic for Applications (VBA) codes can be included as a…

Functions (Excel)

In this section Using the Solver VBA Functions SolverAdd Function SolverChange Function SolverDelete Function SolverFinish Function…

Hide and Unhide Columns

This example finds all the cells in the first four columns that have a constant "X"…

Highlight the Active Cell, Row, or Column

The following code examples show ways to highlight the active cell or the rows and columns…

How do I... (Excel VBA reference)

This topic links to programming tasks (how-to and walkthrough topics) for common Excel scenarios. "How Do…

Initializing Control Properties

You can initialize controlsat run time by using Visual Basic code in a macro. For example,…

Invalid procedure name error

You have either specified an invalid procedure name or did not include a procedure name when…

List of worksheet functions available to Visual Basic

The following list represents all of the worksheet functions that can be called using the WorkSheetFunction…

List of worksheet functions that support long strings

The following list represents all the worksheet functions that support strings longer than 255 characters. The…

Looping Through a Range of Cells

When using Visual Basic, you often need to run the same block of statements on each…

Make a Cell Blink

This example shows how to make cell B2 on sheet 1 blink by changing the color…

PivotTable.HasAutoFormat property (Excel)pivottable-hasautoformat-property-excel-638f892d-7dc9-314c-efaf-ee724b9f86f5

pivottable-hasautoformat-property-excel-638f892d-7dc9-314c-efaf-ee724b9f86f5 !include[Support and feedback]

PivotTable.ShowTableStyleLastColumn property (Excel)pivottable-showtablestylelastcolumn-property-excel-63370f64-5188-50fd-3e8a-6fdb0

pivottable-showtablestylelastcolumn-property-excel-63370f64-5188-50fd-3e8a-6fdb0 !include[Support and feedback]

Prevent Duplicate Entries in a Range

The following code example verifies that a value entered in the range A1:B20 exists within that…

QueryTable object events

QueryTable object events occur when a QueryTable object is refreshed. These events are enabled by default.…

Refer to All the Cells on the Worksheet

When you apply the Cells property to a worksheet without specifying an index number, the method…

Refer to Cells and Ranges by Using A1 Notation

Refer to a cell or range of cells in the A1 reference style by using the…

Refer to Cells by Using a Range Object

If you set an object variable to a Range object, you can easily manipulate the range…

Refer to Cells by Using Index Numbers

Use the Cells property to refer to a single cell by using row and column index…

Refer to Cells by Using Shortcut Notation

Use either the A1 reference style or a named range within brackets as a shortcut for…

Refer to Cells Relative to Other Cells

A common way to work with a cell relative to another cell is to use the…

Refer to Multiple Ranges

By using the appropriate method, you can easily refer to multiple ranges. Use the Range and…

Refer to Named Ranges

Ranges are easier to identify by name than by A1 notation. To name a selected range,…

Refer to Rows and Columns

Use the Rows property or the Columns property to work with entire rows or columns. These…

Reference cells and ranges

You can specify a cell or range of cells in one statement that identifies the range…

Select a Range

These examples show how to select the used range, which includes formatted cells that don't contain…

Outlook

30 reference pages

Access Exchange User or Distribution List Information from the Address Book

This topic describes the objects that support accessing information about an Exchange user or distribution list…

Add a Command to the Ribbon to Start a Reply Form

Use this procedure to customize a Microsoft Outlook form page. To customize a form region, see…

Assign a Picture to a Form

1. In the Forms Designer, click the tab for the page or the form region that…

Attach a File to an Outlook Email Message

This topic describes how to programmatically attach one or more files to an outgoing email message…

Attach an Outlook Contact Item to an Email Message

This topic describes how you can programmatically attach a copy of a Microsoft Outlook item, such…

Automatically Insert Prefix Text into the Subject Field of a Reply Form

Customizing with form regions In a form region, you can set the subject prefix in the…

Categorize Your Outlook Items

Microsoft Outlook provides color categorization functionality, in which Outlook items can be categorized and displayed by…

Change the Default Form for a Folder

You can change the default form for a particular public folder so that when a new…

Change the Icons Associated with your Form

Forms customized with form regions When you customize icons on a form region, you can set…

Create a Sendable Item for a Specific Account Based on the Current Folder (Outlook)

When you use the CreateItem method of the Application object to create a Microsoft Outlook item,…

Creating a Reply Form

When you create a message form, you often want users to send a reply by using…

Disable a Standard Reply Form

Use this procedure to customize a form page in Microsoft Outlook. To customize a form region,…

Display a Dialog Box for Selecting Entries from the Contacts Folder

This topic describes how to use the SelectNamesDialog object to display entries from the Contacts folder…

Display Address Entry Details for the Sender of a Message

The recipient of each mail message deliverable by a transport provider has an address entry in…

Display Names from the Address Book

This topic describes the address book and explains how to programmatically display names from an address…

Hide or Show Form Pages

When you customize an Outlook form, the procedure that you use for form pages is different…

Identify a Folder with an Account

In a Microsoft Outlook session that has multiple accounts defined in the profile, the folder that…

Identify the Global Address List or a Set of Address Lists with a Store

In a Microsoft Outlook session where multiple Microsoft Exchange accounts are defined in the profile, there…

Limit the Size of an Attachment to an Outlook Email Message

This topic describes how you can create a managed add-in for Outlook that cancels sending email…

List the Groups that My Manager Belongs to

This topic describes how to obtain the names of the Exchange distribution lists that the manager…

List the Name and Office Location of Each Manager Belonging to an Exchange Distribution List

This topic describes how to allow a user to select an Exchange distribution list and display…

Make Separate Compose and Read Versions for a Form

You can create and edit separate compose and read versions for each page of a form.…

Managing Outlook Items as Conversations

In Microsoft Outlook, a conversation groups messages that share the same subject and belong to the…

Map a Display Name to an Email Address

This topic shows a code sample in Visual Basic for Applications (VBA) that takes a display…

Modify an Attachment of an Outlook Email Message

This topic describes how you can programmatically modify a Microsoft Outlook email attachment without changing the…

Obtain and Enumerate Selected Conversations

By default, Microsoft Outlook displays items in the Inbox by conversation. If a user makes a…

Obtain Information for Multiple Accounts

Microsoft Outlook supports a profile that contains one or more accounts that are connected to a…

Obtain the Email Address of a Recipient

This topic shows how to obtain the SMTP address for each recipient in a Recipients collection.…

Programmatically Change the Display Format for All Contacts

In Outlook, you can specify the default setting for how to file new contacts through the…

Using Multiple Accounts for the Same Profile on Outlook

You can sign in to Outlook by using a profile that specifies one or more accounts…

Word

30 reference pages

Adding Controls to a Document

You can add ActiveX controls to a document's drawing layer or text layer. To add a…

Adding Controls to a UserForm

To add ActiveX controls to a user form, find the control you want to add in…

Applying formatting to text

This topic includes Visual Basic examples related to the tasks in the following sections. Applying formatting…

Assigning Ranges

There are several ways to assign an existing Range object to a variable. This topic explains…

Auto Macros

By giving a macro a special name, you can run it automatically when you perform an…

Automating Common Word Tasks

This topic includes some common Word tasks and the Visual Basic code needed to accomplish the…

Built-in Dialog Box Argument Lists (Word)

Many of the built-in dialog boxes in Word have options that you may want to set.…

Communicating with Other Applications

In addition to working with Word data, you may want your application to exchange data with…

Conceptual Differences Between WordBasic and Visual Basic

The primary difference between Visual Basic for Applications (VBA) and WordBasic is that, whereas the WordBasic…

Control and Dialog Box Events (Word)

After you have added ActiveX controlsto your dialog box or document, you add event procedures to…

Converting WordBasic Macros to Visual Basic

Word 2003 and Word 2007 automatically convert the macros in a Word 6.x or Word 95…

Creating a Custom Dialog Box

Use the following procedure to create a custom dialog box: 1. Create a UserForm On the…

Creating a UserForm

To create a custom dialog box, you must create a UserForm. To create a UserForm, click…

Creating Frames Pages

In Word, you can use frames in your Web page design to make your information organized…

Determining Whether the Application Property Is Necessary

Many of the properties and methods of the Application object can be used without the Application…

Displaying a Custom Dialog Box (Word)

To test your dialog box in the Visual Basic Editor, click Run Sub/UserForm on the Run…

Displaying built-in Word dialog boxes

This topic contains information and examples in the following sections. Showing a built-in dialog box You…

Editing text (Word)

This topic includes Visual Basic examples related to the tasks identified in the following sections. Determine…

Error Accessing a Table Row or Column

When you try to access an individual row or column in a drawn table, a run-time…

Finding and Replacing Text or Formatting

Finding and replacing is exposed by the Find and Replacement objects. The Find object is available…

Finding Out Which Property or Method to Use

Use the macro recorder to learn which methods or properties you need to accomplish a task…

Frequently Asked Visual Basic Questions

General questions How do I record macros? What are objects, properties, and methods? How do I…

Initializing Control Properties (Word)

You can initialize ActiveX controls at run time by using Visual Basic code in a macro.…

Inserting Text in a Document

Use the InsertBefore method or the InsertAfter method of the Selection object or the Range object…

Object Doesn't Support this Property or Method

The "object doesn't support this property or method" error occurs when you try to use a…

Using events with the Application object (Word)

To create an event handler for an event of the Application object, you need to complete…

Using Events with the Document Object

The Document object supports several events that enable you to respond to the state of a…

Working with Range Objects

A common task when using Visual Basic is to specify an area in a document and…

Working with the Selection Object

When you work on a document in Word, you usually select text and then perform an…

Working with the UndoRecord Object

As end users work in Word, the actions they take that can be undone are recorded…