"For Each" loop doesn't get everything

M

Michael Pollard

I have a "For Each" loop that gets most of the items the first time around,
and the rest the second time. It takes two loops to get everything. ("Each"
doesn't get each "each".)

Each object has a custom property that potentially refers to another object
by ID; if that other object is there, I want to delete it. The objects missed
are almost consistent; they are consistent if the selection is the same, but
some items appear to vary depending on the other selected items. Just for
confirmation, nothing is going on between the two loops; I just click Play in
the Editor each time. And I have 2003 Pro.

Please let me know if you see a logic error in the function. (I may have
made a "stupid" mistake...) Also, feel free to let me know if there is a
better way of doing it.

My code:

Public Function DeleteAllBackgroundBoxes()

Dim CurrentObject As Object
' Loop through all objects
For Each CurrentObject In Application.ActivePage.Shapes
' Determine if the background ID property exists
If CurrentObject.CellExistsU("Prop.BackgroundObjectID", False) Then
If Not
((CurrentObject.CellsU("Prop.BackgroundObjectID").FormulaU = "") Or _
(CurrentObject.CellsU("Prop.BackgroundObjectID").FormulaU =
0)) Then
' If so, delete the background and the background ID

ActivePage.Shapes.ItemFromID(CurrentObject.CellsU("Prop.BackgroundObjectID")).Delete
CurrentObject.CellsU("Prop.BackgroundObjectID").FormulaU = ""
End If
End If
Next
End Function
 
P

Paul Herber

I have a "For Each" loop that gets most of the items the first time around,
and the rest the second time. It takes two loops to get everything. ("Each"
doesn't get each "each".)

Each object has a custom property that potentially refers to another object
by ID; if that other object is there, I want to delete it. The objects missed
are almost consistent; they are consistent if the selection is the same, but
some items appear to vary depending on the other selected items. Just for
confirmation, nothing is going on between the two loops; I just click Play in
the Editor each time. And I have 2003 Pro.

If you have a collection of, say, 10 objects and delete number 5 then
objects 6 to 10 will take the place of objects 5 to 9 and object 10
will be removed.
After the delete the for ech counter resumes at object 6 but that
object is at 5, hence it gets missed.

Try using a standard For loop but iterate throught the collection
backwards i.e. from Object.Count to 1 step -1
 
M

Michael Pollard

So the "For Each" loop actually uses a hidden "From...To" loop that can be
broken... I guess that ought to be reported as a bug to MS, if it hasn't.

Anyway, thanks for the workaround.
 
P

Paul Herber

Any loop can be broken if the indices of the object being iterated are
modified. This applies to For Each, For ... Next, Repeat, While etc
And by object here I don't just mean Object as in OO. It applies to
whatever is being counted.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top