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: Understanding the For ... Next Structure.

Understanding the For ... Next Structure

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


Like other programming languages, the programming language used for Word macros (Visual Basic for Applications, or VBA) includes certain programming structures used to control how the program executes. One of these structures is the For ... Next structure. The most common use of this structure has the following syntax:

For X = 1 To 99
    program statements
Next X

You are not limited to using the X variable; you can use any numeric variable you desire. You are also not limited to the numbers 1 and 99 in the first line; you can use any numbers you desire, or you can use numeric variables. When a macro is executing, and this structure is encountered, Word repeats every program statement between the For and Next keywords a certain number of times. In the syntax example, the statements would be executed 99 times (1 through 99). The first time through the structure, X would be equal to 1, the second time through it would be equal to 2, then 3, 4, 5, and so on, until it equaled 99 on the last iteration.

Normally, as Word is working through the For ... Next structure, it increments the counter by one on each iteration. You can also add a Step modifier to the For ... Next structure, thereby changing the value by which the counter is incremented. For instance, consider the following example:

For X = 1 To 99 Step 5
    program statements
Next X

The first time through this example, X will be equal to 1, and the second time through, X is equal to 6 because it has been incremented by 5. Similarly, the third time through X is equal to 11. You can also use negative numbers for Step values, which allows you to count downwards. For instance, take a look at the following:

For X = 24 To 0 Step -3
    program statements
Next X

In this example, the first time through the structure X is equal to 24, the second time it is equal to 21, and the third time it is equal to 18.

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 (12215) 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: Understanding the For ... Next Structure.

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

File Sizes in Word

The size of files created by Word depends on the version of the program you are using. Here's an analysis of the minimum ...

Discover More

Editing a Scenario

Once a scenario is defined and saved, you can later revisit the values you created for the scenario and modify them. ...

Discover More

Inconsistent Availability of Drag and Drop Editing

Some people really like the ability to drag and drop text as they edit. What do you do if the ability is only available ...

Discover More

The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!

More WordTips (ribbon)

Continuing Macro Lines

Program a macro, and you can easily find that some lines get very long. If you want to shorten the lines so they are more ...

Discover More

Finding a Change in Typeface

When you format a document, you can go so far as to change the font of each character in the document. This may be ...

Discover More

Automatically Inserting Tomorrow's Date

Do you routinely need to work with tomorrow's date? Why not create a template that automatically adds tomorrow's date to ...

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 1 + 1?

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