Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, 2016, 2019, and Word in Office 365. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Declaring Variables.
by Allen Wyatt
(last updated August 18, 2018)
If you have ever programmed any macros, you are probably familiar with how you define variables using the Dim keyword. For instance, you can define an integer variable with the name MyVar as follows:
Dim MyVar As Integer
This is very straightforward, and will work fine in your code. You may be tempted to define multiple variables per line, however:
Dim x, y, z As Integer
In some versions of the BASIC language, this will define and initialize three variables, each as an integer. In VBA it also appears to run properly, and no error is generated. However, there is a small problem—only the last variable (z) is actually defined as an integer. You can see how this works by using the following code:
Sub DimTest() Dim x, y, z As Integer MsgBox "x is type " & VarType(x) MsgBox "y is type " & VarType(y) MsgBox "z is type " & VarType(z) End Sub
When you run the macro, the first two message boxes that pop up show that the variable type for x and y are 0, which means uninitialized. Only the last message box (for z) shows a variable type of 2, meaning an integer.
The solution is to make sure that you declare your variables one per line, or using the full syntax for each variable, as in the following:
Dim x As Integer, y As Integer, z As Integer
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (12098) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, and Word in Office 365. You can find a version of this tip for the older menu interface of Word here: Declaring Variables.
Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!
If you need to quickly switch a text selection from one typeface to another, one way you can do it is with a macro. This ...Discover More
Want the ribbon to be minimized for a particular document? Word may not allow you to get the exact result you want, as ...Discover More
Want to turn off document repagination while your macro does its work? Here are two approaches you can use.Discover More
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.