Visual Basic Scripting Edition
For Each...Next Statement

Repeats a group of statements for each element in an array or collection.

For Each element In group
   [statements]
   [Exit For]
   [statements]
Next [element]
Arguments

element

Variable used to iterate through the elements of the collection or array. For collections, element can only be a Variant variable, a generic Object variable, or any specific Automation object variable. For arrays, element can only be a Variant variable.

group

Name of an object collection or array.

statements

One or more statements that are executed on each item in group.

Remarks

The For Each block is entered if there is at least one element in group. Once the loop has been entered, all the statements in the loop are executed for the first element in group. As long as there are more elements in group, the statements in the loop continue to execute for each element. When there are no more elements in group, the loop is exited and execution continues with the statement following the Next statement.

The Exit For can only be used within a For Each...Next or For...Next control structure to provide an alternate way to exit. Any number of Exit For statements may be placed anywhere in the loop. The Exit For is often used with the evaluation of some condition (for example, If...Then), and transfers control to the statement immediately following Next.

You can nest For Each...Next loops by placing one For Each...Next loop within another. However, each loop element must be unique.

NoteNote:

If you omit element in a Next statement, execution continues as if you had included it. If a Next statement is encountered before it's corresponding For statement, an error occurs.

The following example illustrates use of the For Each...Next statement:

Function ShowFolderList(folderspec)
   Dim fso, f, f1, fc, s
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.GetFolder(folderspec)
   Set fc = f.Files
   For Each f1 in fc
      s = s & f1.name 
      s = s & "<BR>"
   Next
   ShowFolderList = s
End Function
Requirements

Version 2

See Also

Reference

Tags :


Community Content

Thomas Lee
Pitfall on "For Each" Loop
This next tip is confirmed for Visual Basic only, i do not know if any other programming language has the same effect

The "For Each" loop will execute for every element in, for instance a array. However i found out the hard way that the "For Each" loop
cant be used to edit it's self. The next code explains:


Consider the next code:



Dim MyArray(255) As Integer

<--- any proces that fills the array MyArray with data --->

For Each Element In MyArray
Element = Element / 100
Next Element



Now one would suspect that the entire array is now devided by 100.
It's not, a big pitfall for the beginners like me. Infact the data in MyArray is untouched!!!
The only way would be to make a "For X = 0 To 255, next" loop see next example




For X = 0 To 255
MyArray(X) = MyArray(X) / 100
Next




Not that spectacular difference but it makes all the difference between a working subroutine or not :D

Tags : foreach

Colonel Gizmo
Re: Pitfall on "For Each" Loop
This behavior is really simple to explain:

The 'For Each' statement allows only READ ONLY access for the enumerated elements - NO write access.

Greetings
Col. Gizmo
Tags : foreach

Page view tracker