Reputation: 123
I have two charts in a range of cells that I am trying to delete with VBA. Rather than deleting the charts, it iterates through a bunch of times without doing anything at all. This is what I have tried so far.
For Each ChartObjects In Range(Cells(i + 3, 12), Cells(i + 19, 50))
ChartObjects.Delete
Next ChartObjects
Upvotes: 0
Views: 1648
Reputation: 8114
You can use the TopLeftCell property of the ChartObject to check whether the cell under the top left corner of the chart falls within the range...
Dim oChrtObj As ChartObject
For Each oChrtObj In ActiveSheet.ChartObjects
If Not Application.Intersect(oChrtObj.TopLeftCell, _
Range(Cells(i + 3, 12), Cells(i + 19, 50))) Is Nothing Then
oChrtObj.Delete
End If
Next oChrtObj
Note, though, you can also delete all charts using a single line...
activesheet.chartobjects.delete
Hope this helps!
Upvotes: 1