So we still await a definitive disquisition on the subject of why Loop
Until Object is Nothing is superior to For Each Object, and why the
Loop Until Object is Nothing method doesn't touch some members of a
collection?
Students at the Monkey See Monkey Do Institute are eager for a lecture.
Hello, this is Monkey Hear Monkey Say calling Monkey See Monkey Do...
The reasons that I have heard for using Next-Until-Nothing rather than
For-Each:
1.
For large collections, For-Each allegedly becomes slower the farther
you get into the collection. The implication is that it walks down the
list from the beginning each time in order to find the "next" element.
Next-Until-Nothing starts from where you are, so it should not suffer
this problem.
(There have also been claims that for large collections a For-Each is
much faster than an indexed For-To loop.)
2.
According to the documentation, a For-Each loop is supposed to process
each member of the collection once, even if you alter the content of
the members in the loop. (I don't know if it is supposed to do it
"exactly once" or "at least once".) For example, if you do a For Each
Paragraph loop, it is supposed to do each paragraph even if you are
deleting some of the characters from the paragraph as part of the code
in the loop.
However, there have been several postings in the VBA groups that
appear to show that such loops may restart from the beginning if the
loop body alters the content of a member. This can result in the loop
never finishing, or making excessive alterations to the text.
Exactly what mechanism VBA is using to attempt to cause the For-Each
to hit each member exactly once is a mystery to me.
Whatever mechanism it uses is known to fail in the face of collection
re-shuffling caused by deleting a member; maybe it also fails in
certain other cases. I haven't seen an official list of what can cause
collection re-ordering.
Bob S