Find more freelance jobs
|
You can use a counter variable that increases or decreases with each repetition of the loop, like this:
For i = 1 to 10 some code Next The For statement specifies the counter variable i and its start and end values. The Next statement increases i by 1. Using the Step keyword, you can increase or decrease the counter variable by the value you specify. For i = 2 To 10 Step 2 some code Next In the example above, i is increased by 2 each time the loop repeats. When the loop is finished, total is the sum of 2, 4, 6, 8, and 10. To decrease the counter variable, you use a negative Step value. You must specify an end value that is less than the start value. For i = 10 To 2 Step -2 some code Next In the example above, i is decreased by 2 each time the loop repeats. When the loop is finished, total is the sum of 10, 8, 6, 4, and 2. Exiting a For...Next You can exit a For...Next statement with the Exit For keyword. For Each...Next A For Each...Next loop repeats a block of code for each item in a collection, or for each element of an array. The For Each...Next statement looks almost identical to the For...Next statement. The difference is that you do not have to specify the number of items you want to loop through. dim names(3) names(0) = "Tove" names(1) = "Jani" names(2) = "Hege" For Each item in names some code Next |