admin管理员组文章数量:1125305
I have many PPT files, each PPT file has many slides and in each slides, there are many shapes.i want to delete the 2 shapes with the name "Google Shape;227;p3" and "Google Shape;228;p3" in each slide respectively.
i tried 2 macros differently for each shape like
Sub DeleteLogo()
Dim n As Integer
Dim shp As Shape
Dim sld As Slide
For n = 1 To ActivePresentation.Slides.Count
For Each shp In ActivePresentation.Slides(n).Shapes
If shp.Name = "Google Shape;227;p3" Then shp.Delete
Next
Next n
End Sub
and it worked fine for 1 shape only, but when i tried to combine to delete 2 shapes in the same macro like
Sub DeleteLogo()
Dim n As Integer
Dim shp As Shape
Dim sld As Slide
For n = 1 To ActivePresentation.Slides.Count
For Each shp In ActivePresentation.Slides(n).Shapes
If shp.Name = "Google Shape;227;p3" Then shp.Delete
If shp.Name = "Google Shape;228;p3" Then shp.Delete
Next
Next n
End Sub
it failed and said
enter image description here where does the logic go wrong ? please help.
explanation for illogical code
I have many PPT files, each PPT file has many slides and in each slides, there are many shapes.i want to delete the 2 shapes with the name "Google Shape;227;p3" and "Google Shape;228;p3" in each slide respectively.
i tried 2 macros differently for each shape like
Sub DeleteLogo()
Dim n As Integer
Dim shp As Shape
Dim sld As Slide
For n = 1 To ActivePresentation.Slides.Count
For Each shp In ActivePresentation.Slides(n).Shapes
If shp.Name = "Google Shape;227;p3" Then shp.Delete
Next
Next n
End Sub
and it worked fine for 1 shape only, but when i tried to combine to delete 2 shapes in the same macro like
Sub DeleteLogo()
Dim n As Integer
Dim shp As Shape
Dim sld As Slide
For n = 1 To ActivePresentation.Slides.Count
For Each shp In ActivePresentation.Slides(n).Shapes
If shp.Name = "Google Shape;227;p3" Then shp.Delete
If shp.Name = "Google Shape;228;p3" Then shp.Delete
Next
Next n
End Sub
it failed and said
enter image description here where does the logic go wrong ? please help.
explanation for illogical code
Share Improve this question asked 2 days ago Thanh Tung DoanThanh Tung Doan 1 New contributor Thanh Tung Doan is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 1- second macro arises because the loop gets disrupted when you delete a shape. – Hasan Raza Commented 2 days ago
1 Answer
Reset to default 0Problem : second macro arises because the loop gets disrupted when you delete a shape.
Solution : iterate through the Shapes collection in reverse order
Sub DeleteLogo()
Dim n As Integer
Dim shp As Shape
Dim sld As Slide
Dim i As Integer
For n = 1 To ActivePresentation.Slides.Count
Set sld = ActivePresentation.Slides(n)
For i = sld.Shapes.Count To 1 Step -1
Set shp = sld.Shapes(i)
If shp.Name = "Google Shape;227;p3" Or shp.Name = "Google Shape;228;p3" Then
shp.Delete
End If
Next i
Next n
End Sub
本文标签: loopsDelete the same shapes in each slidesStack Overflow
版权声明:本文标题:loops - Delete the same shapes in each slides - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736657801a1946299.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论