How to Create a For Next Loop in Excel VBA

By Keith Johnson

Rate: (10 Ratings)

The For Next loop is one of the most useful loops in Excel VBA. It allows you to run the same code a specific number of times--and it's easy to learn.

Instructions

Difficulty: Moderately Easy
Step1
Start the Visual Basic Editor and insert a new module.
Step2
Create a new subroutine.
Step3
First, declare a counter variable. For example: Dim m as integer. This variable will keep track of the number of times you run your code. Press Enter.
Step4
Type “For” and give the variable a starting value--usually 1. For example: For m = 1.
Step5
Next, type “To” and the number of times you want to run the code. This is your ending value. For example: For m = 1 To 5. Press Enter.
Step6
Type the code you want to run within the loop. For example: MsgBox ("Hello world!"). Press Enter.
Step7
Type “Next” and the counter variable's name. For example: Next m. This statement will automatically increment the variable until the code within the loop is run the specified number of times.
Step8
Run the program.

Post a Comment

POST A COMMENT

Request a New How-To Article

Looking for more How To information? Chances are there’s an eHow member who knows how to do what you’re looking to do. Submit an article request now!

eHow Article: How to Create a For Next Loop in Excel VBA

Article By: Keith Johnson

Keith Johnson

Authority Authority | 3000 Points

Category: Computers

Articles: See my other articles

Related Ads