Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Understanding Variables in VBA Macros.

Understanding Variables in VBA Macros

Written by Allen Wyatt (last updated April 9, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


Excel allows you to write macros in a language called Visual Basic for Applications (VBA). This is a specialized version of the BASIC programming language, and as such, allows you to use variables. Variables are nothing but names which represent other data. During the course of your macro you can even change the data to which the name applies.

VBA allows you to use quite a few different types of variables. There are eleven types of variables you can use in your macros. These are known as data types, and you should use the data type that most closely matches the characteristics of the information you are storing in the variable. VBA supports the following data types:

  • Byte. A numeric variable within the range of 0 to 255.
  • Boolean. A variable with two possible values: True (-1) or False (0).
  • Integer. A numeric variable designed for whole numbers in the range of -32,768 to 32,767.
  • Long. A numeric variable designed for very large whole numbers.
  • Currency. A numeric variable designed for calculations involving monetary values.
  • Single. A numeric variable designed for single-precision floating-point values; accurate to about six or seven decimal places.
  • Double. A numeric variable designed for double-precision floating-point values; accurate to about 15 decimal places.
  • Date. A numeric variable designed to represent a date and time as a real number. The value to the left of the decimal point is the date, and that portion to the right of the decimal point is the time.
  • String. A variable that can contain any type of text or character you desire. You can assign a maximum of approximately 63,000 characters to a string variable.
  • Object. A variable that contains a pointer to a defined object within VBA.
  • Variant. A variable that can contain any type of data.

An additional data type (Decimal) is also specified in the VBA documentation, but is not currently supported by the language. As in other versions of BASIC, VBA also allows you to define variable arrays and you can also create user-defined data types. The full range of variable specifications is much too complex for a simple ExcelTip, however. If you need specific information about how to work with variables, refer to a good Visual Basic or VBA programming book. You can also look in the VBA on-line help under the Dim statement.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11800) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Understanding Variables in VBA Macros.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Embedding TrueType Fonts by Default

If you use TrueType fonts frequently, you might want to set Word to embed those fonts by default. Here's how to do it.

Discover More

Returning the Smallest Non-Zero Value

In a series of values, you may need to know the smallest value that isn't a zero. There is no built-in function to do ...

Discover More

Double-Spacing Your Document

Need to produce a quick double-spaced printout of your document? You can do it by using the simple steps in this tip.

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Calling a Subroutine from a UDF

Excel allows you to create a special type of macro called a user-defined function (UDF). These can let you add to the ...

Discover More

Declaring Variables

Macros depend on the use of variables to do their work. This tip examines how variables are declared in a macro, using ...

Discover More

Turning Off Screen Updating

Want a quick way to speed up your macros? All you need to do is to stop Excel from updating the screen while the macro is ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 2 + 8?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.