For...Next statement
Repeats a group of statements a specified number of times.
Syntax
For counter = start To end [ Step step ]
[ statements ]
[ Exit For ]
[ statements ]
Next [ counter ]
The For…Next statement syntax has these parts:
Part | Description |
---|---|
counter | Required. Numeric variable used as a loop counter. The variable can't be a Boolean or an array element. |
start | Required. Initial value of counter. |
end | Required. Final value of counter. |
step | Optional. Amount counter is changed each time through the loop. If not specified, step defaults to one. |
statements | Optional. One or more statements between For and Next that are executed the specified number of times. |
Remarks
The step argument can be either positive or negative. The value of the step argument determines loop processing as follows.
Value | Loop executes if |
---|---|
Positive or 0 | counter <= end |
Negative | counter >= end |
After all statements in the loop have executed, step is added to counter. At this point, either the statements in the loop execute again (based on the same test that caused the loop to execute initially), or the loop is exited and execution continues with the statement following the Next statement.
Tip
Changing the value of counter while inside a loop can make it more difficult to read and debug your code.
Any number of Exit For statements may be placed anywhere in the loop as an alternate way to exit. Exit For is often used after evaluating some condition, for example If...Then, and transfers control to the statement immediately following Next.
You can nest For...Next loops by placing one For...Next loop within another. Give each loop a unique variable name as its counter. The following construction is correct:
For I = 1 To 10
For J = 1 To 10
For K = 1 To 10
...
Next K
Next J
Next I
Note
If you omit counter in a Next statement, execution continues as if counter is included. If a Next statement is encountered before its corresponding For statement, an error occurs.
Example
This example uses the For...Next statement to create a string that contains 10 instances of the numbers 0 through 9, each string separated from the other by a single space. The outer loop uses a loop counter variable that is decremented each time through the loop.
Dim Words, Chars, MyString
For Words = 10 To 1 Step -1 ' Set up 10 repetitions.
For Chars = 0 To 9 ' Set up 10 repetitions.
MyString = MyString & Chars ' Append number to string.
Next Chars ' Increment counter
MyString = MyString & " " ' Append a space.
Next Words
See also
- Making faster For...Next loops
- Using For...Next statements
- For Each...Next statement
- Data types
- Statements
Support and feedback
Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기