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

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
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,224,740
Messages
6,180,678
Members
452,993
Latest member
FDARYABEE

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