For each... the next loop is similar to the for... next loop. For each... next does not mean to run the specified number of times, but repeats a group of statements for each element or object set in the array. This is useful when you do not know the number of elements in the collection.
The syntax structure is as follows:
| 12345 |
For Each element In group [statements] [Exit For] [statements]Next [element] |
Main parameters:
Element: Used to enumerate variables of all elements in a set or array. For a set, element may be a variant variable, a common object variable, or any specified automation object variable. For arrays, element can only be a variant variable.
Group: Name of the object set or array.
The specific instance code is as follows:
| 12345678 |
Dim Dics 'Defines a variableSet Dics = CreateObject("Scripting.Dictionary") 'Define a dictionary objectDics.Add "0", "Athens"'Assigns a value to the dictionary objectDics.Add "1", "Belgrade"Dics.Add "2", "Cairo"For Each Dic in Dics MsgBox Dics.Item(Dic) 'Looping and outputting the dictionary key valueNext |