Deleting Lines and Rectangles

adrian.groves

New Member
Joined
Aug 29, 2012
Messages
18
Hi,

I have a need to delete many shapes on a worksheet although I wish to keep others. All of the shapes that I wish to delete are either lines or rectangles and the ones that I wish to keep are not. I had hoped the following would work, but it doess not - can anybody let me know what the correct code should be please?

Dim shp As Shape
For Each shp In ActiveSheet.Shapes
If shp.LineShape = Line Then shp.Delete
If shp.LineShape = Rectangle Then shp.Delete
Next shp

Thanks
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
How about
Code:
Sub adriangroves()
Dim Shp As Shape
For Each Shp In ActiveSheet.Shapes
  If Shp.AutoShapeType = -2 Or Shp.AutoShapeType = 1 Then Shp.Delete
Next Shp
End Sub
 
Upvote 0
Thanks Fluff,

That is almost there but it also deleted the text boxes and curly brackets that I had. I played around and noted the -2 was the problem..... do you have any further suggestions.

Thanks
 
Upvote 0
It shouldn't have delete the brackets, but the textbox yes.
Have you renamed any of the shapes?
 
Upvote 0
If it makes any difference, I had drawn the lines using the following VBA code

Shapes.AddLine(Startx, Starty, Endx, Endy)

and drawn the rectangles using

Shapes.AddShape(1, .....
 
Upvote 0
Ok, how about
Code:
Sub adriangroves()
Dim Shp As Shape
For Each Shp In ActiveSheet.Shapes
   If Shp.Name Like "Straight*" Or Shp.Name Like "Rectangle*" Then Shp.Delete
Next Shp
End Sub
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top