How To

How to Create a For Next Loop in Excel VBA

Member
By Keith Johnson
User-Submitted Article
(18 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.

Difficulty: Moderately Easy
Instructions
  1. Step 1

    Start the Visual Basic Editor and insert a new module.

  2. Step 2

    Create a new subroutine.

  3. Step 3

    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.

  4. Step 4

    Type "For" and give the variable a starting value--usually 1. For example: For m = 1.

  5. Step 5

    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.

  6. Step 6

    Type the code you want to run within the loop. For example: MsgBox ("Hello world!"). Press Enter.

  7. Step 7

    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.

  8. Step 8

    Run the program.

Subscribe

Post a Comment

Post a Comment

Related Ads

  • Have you done this? Click here to let us know.
I Did This
Get Free Computers Newsletters

Copyright © 1999-2010 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy .   en-US † requires javascript

eHow Computers
eHow_eHow Technology and Electronics