Understanding the For ... Next Structure

Written by Allen Wyatt (last updated September 17, 2020)
This tip applies to Excel 97, 2000, 2002, and 2003


1

Macros in Excel are written in a language called Visual Basic for Applications (VBA). Like any other programming language, VBA includes certain programming structures which are 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, Excel 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 Excel 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 ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2024) applies to Microsoft Excel 97, 2000, 2002, and 2003.

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

Turning On Picture Placeholders

Displaying graphics in a document requires a great deal more computer processing than displaying simple text. A document ...

Discover More

Creating a Document Font List

If you want a list of all the fonts used in a document, the answer isn't as simple as you may think. This tip uses macros ...

Discover More

Working with Table Columns and Rows

Need to add or delete columns and rows from a table? It's easy to do using the tools provided in Word.

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (menu)

Telling which Worksheets are Selected

If your macro processes information on a number of worksheets, chances are good that you need your macro to figure out ...

Discover More

Understanding Macros

What is a macro? Ever wonder what these are and how to use them? This tip answers the basics of what a macro is used for, ...

Discover More

Error Using ATAN2 Function in Macro

Excel allows you to use worksheet functions from within macros. This is helpful, especially when you are trying to ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, 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 8 - 5?

2023-05-14 21:53:08

slh9001@yahoo.com

On your website looking for your Macros eBook. I could definitely use some tips for Macros.

I just wanted to let you know that your tips are very helpful. I was not even aware that some of these functions/actions were available. Thank you so much.


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

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