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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
31,992
Office Version
365
Platform
Windows
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
 

adrian.groves

New Member
Joined
Aug 29, 2012
Messages
18
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
31,992
Office Version
365
Platform
Windows
It shouldn't have delete the brackets, but the textbox yes.
Have you renamed any of the shapes?
 

adrian.groves

New Member
Joined
Aug 29, 2012
Messages
18
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, .....
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
31,992
Office Version
365
Platform
Windows
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
31,992
Office Version
365
Platform
Windows
You're welcome & thanks for the feedback
 

Forum statistics

Threads
1,081,560
Messages
5,359,609
Members
400,538
Latest member
leon_oscar

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top