Please Note: This article is written for users of the following Microsoft Word versions: 97, 2000, 2002, and 2003. If you are using a later version (Word 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Word, click here: Understanding the For ... Next Structure.
Written by Allen Wyatt (last updated September 21, 2019)
This tip applies to Word 97, 2000, 2002, and 2003
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:
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (129) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Understanding the For ... Next Structure.
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!
Macros can be used to read and write all sorts of files. If those files are on a different drive than the current one, ...
Discover MoreIf your macro needs to know how many pages are in your document, you can use the Information method to get the desired ...
Discover MoreNeed to figure out the number of characters in a range of selected text? Here's how to do it in VBA.
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon interface.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments