(VBA) How to loop through a ShapeRange with deleted elements?

Ok, I must be losing memory or something, but what is the correct way to loop through a ShapeRange that changes over time? As an example I have something like this (in pseudocode):

For Each S In SR.Shapes.All

  For Each T In SR.Shapes.All
    If S compared to T is something Then
      Goto Tag

    End If
  Next T
Next S

And no matter what I try:

  • Find the index and do SR.Remove
  • Check if the shapes exist before further actions
  • Use a temp Range from which the shapes are removed and then reapplied to the current Range
  • More stuff I already forgot

it throws an error at some point that the shapes are no longer there. Basically what I want to do here is remove overlapping shapes in the same ShapeRange with specific parameters.

Any suggestions?

No Data
  • no need to use SR.Shapes.All or SR.Shapes  (For Each s In sr)
    to check if shape exists in the document you can set error handling (On Error Goto ...) and try to move it (s.Move 0.0)
    try to use less Goto, use Exit For instead
    you can also use loops with counters, for example:

    For z = 1 To sr.Count - 1
        For i = z + 1 To sr.Count 'use next shape and all subsequent ones
            If sr(z) =compare= sr(i) then Exit For  'goto after Next i
        Next i
    Next z