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

Quickly Clearing Array Contents

Written by Allen Wyatt (last updated September 22, 2023)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365


2

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.

Note:

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

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, 2013, 2016, 2019, and Word in Microsoft 365. 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

Specifying Chart Sizes

If you need a number of charts in your workbook to all be the same size, it can be a bother to manually change each of ...

Discover More

Status Bar Summing No Longer Available

When you select a range of cells, Excel normally displays the sum of those selected cells on the status bar. If the sum ...

Discover More

Throwing Out the Lowest Score

Want to add up a bunch of scores, without including the lowest one in the bunch? You can make a small change to your ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More WordTips (ribbon)

Converting Strings to Numbers

When creating macros, you often need to convert a text string that contains numbers into actual numeric values. You do ...

Discover More

Determining a Paragraph's Style in VBA

When processing a document via a macro, it is often helpful to understand what style has been applied to a paragraph. You ...

Discover More

Highlighting Every Other Line

Macros are very good at completing mundane, repetitive tasks. For instance, you could use the macro presented in this tip ...

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

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 nine minus 1?

2021-01-04 14:41:14

Nickie Geldenhuys

Is it possible to create a macro that will do several processes, like "find all sentences beginning with 'And' eliminate that unnecessary conjunction then capitalize the next word, regardless of what that word will be?


2019-08-16 01:05:37

Muhammad Ashfaq

Sir, i want to use thousand separator e.g. 10000000 wants write as 1,00,00,000. thanks while using mail merge tool


This Site

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.

Videos
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.