Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, and 2013. 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: Quickly Clearing Array Contents.

Quickly Clearing Array Contents

by Allen Wyatt
(last updated March 9, 2016)

1

If you have done any programming in VBA, you know the value of using variable arrays to store information. It is not uncommon to start working with large arrays in your macros. For instance, you might declare a 100-element string array, as follows:

Dim MyText(99) As String

As your macro executes, information can be stored and restored in the elements of the array. At some time, you may want to erase all the information in the array. One classic way of doing this is using a For ... Next loop to step through each array element, as follows:

For J = 0 To 99
    MyText(J) = ""
Next J

When the looping is complete, everything has been erased from the array. A quicker way of accomplishing the same task is to use the ERASE function, as follows:

Erase MyText

Once executed, this single line sets each element of the MyText array back to an empty string. If the array is numeric, then each element of the array is set to zero.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (676) applies to Microsoft Word 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Word here: Quickly Clearing Array Contents.

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

Shortcut for Viewing Formulas

If you need to switch between viewing formulas and viewing the results of those formulas, you'll love the keyboard shortcut ...

Discover More

Renaming a File

Need to rename a file in a macro? It's easy to do using the Name command, as discussed in this tip.

Discover More

Working with Imperial Linear Distances

Excel works with decimal values very easily. It is more difficult for the program to work with non-decimal values, such as ...

Discover More

Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!

MORE WORDTIPS (RIBBON)

Adding a Macro to the Quick Access Toolbar

One of the easiest ways to quickly access a macro is to assign it to the Quick Access toolbar. Here's how you can make the ...

Discover More

Adding Automatic Time Stamps

Your computer knows the current date and time, and Word provides ways you can get that date and time into your document. ...

Discover More

Determining the Size of a File

When processing a document using a macro, you may need to know the precise size of a particular file. The way you figure this ...

Discover More
Subscribe

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

View most recent newsletter.

Comments for this tip:

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}] in your comment text. You’ll be prompted to upload your image when you submit the comment. 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 eight more than 7?

2013-07-15 07:30:46

Bryan

Whoa, never knew of the Erase statement! I usually use ReDim:

ReDim MyText(99)

From my 2 minutes of internet/help docs searching, Erase is cleaner than ReDim, but you have to watch out for variable-dimensioned arrays, because Erase clears out the dimensions as well. You will still have to ReDim before using the variable again.


Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, 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.

Links and Sharing
Share