Asked by:
VBA: ForEach vs. For-Loop

Question
-
Hi!
I have a Word document with 2 paragraph with each 1 footnote.
I select the first paragraph with one footnote. When i iterate over the footnotes, i will get both footnotes with the ForEach . But when i use the second for-loop. I get only one footnote.What´s the difference with the loops? When i use C# and Interop, it´s the same behavior.
Thank you!!!
'Application.ActiveDocument.Paragraphs(1).Range.Footnotes.Count is 1!!! For Each f In Application.ActiveDocument.Paragraphs(1).Range.Footnotes MsgBox f.Index ' the loop runs twice; for each footnote in document Next For i = 1 To Application.ActiveDocument.Paragraphs(1).Range.Footnotes.Count MsgBox Application.ActiveDocument.Paragraphs(1).Range.Footnotes(i).Index ' the loop runs one time; for the one footnote in the paragraph Next
All replies
-
The for loop executes a statement or a block of statements repeatedly until a specified expression evaluates to false.
- int j = 0;
- for (int i = 1; i <= 5; i++)
- {
- j = j + i ;
- }
- int j = 0;
- int[] myArr = new int[] { 0, 1, 2, 3, 5, 8, 13 };
- foreach (int i in myArr )
- {
- j = j + i ;
- }
- Using for loop we can iterate a collection in both direction, that is from index 0 to 9 and from 9 to 0. But using for-each loop, the iteration is possible in forward direction only.
- In variable declaration, foreach has five variable declarations (three Int32 integers and two arrays of Int32) while for has only three (two Int32 integers and one Int32 array).
- When it goes to loop through, foreach copies the current array to a new one for the operation. While for doesn't care of that part.
The other advantage of
foreach
is that it works on anyIEnumerable
, where asfor
only makes sense forIList
, where each element actually has an index.However, if you need to use the index of an element, then of course you should be allowed to use a
for
loop. But if you don't need to use an index, having one is just cluttering your code.1 : For Loops executes a block of code until an expression returns false.
1 : ForEach loop executed a block of code through the items in object collections.
2 : For loop can execute with object collections or without any object collections.
2 : ForEach loop can execute with object collections only.
-
In addition to Pranam's response, it looks like Microsoft has good documentation on examples of For...Next loops (For loops) and For Each...Next loops (For Each loops). I find that For Each loops are nice if one wants to process members of an array once. Whereas, For loops can be used for the previous case AND some powerful processing that involves iterating over members multiple times.
For your case, have you performed any debugging to make sure that Application.ActiveDocument.Paragraphs(1).Range.Footnotes.Count contains the expected output?
For...Next Statements
https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/fornext-statement
For Each...Next Statements
https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/for-eachnext-statement
- Edited by Zac Gab Wednesday, October 6, 2021 4:50 PM