Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all shapes in excel worksheet except form controls

Tags:

excel

vba

I have an excel worksheet where a macro tied to a button draws dynamic shapes based on the user input parameters in the worksheet.

I am trying to write a new macro to clean the sheet, or in other words delete all shapes in the worksheet.

I tried using the code below, and it indeed deletes all shapes, however button form controls also get deleted in the process. Is there an easy way to only get rid of the shapes (arrows, textboxes, ellipses etc.) in the worksheet? Thanks a bunch!!!

Sub DeleteAllShapes()

Dim Shp As Shape

For Each Shp In ActiveSheet.Shapes
    Shp.Delete
Next Shp

End Sub
like image 411
marillion Avatar asked Oct 08 '12 21:10

marillion


People also ask

How do I remove shapes from Excel formatting?

Click the line, connector, or shape that you want to delete, and then press Delete. If you want to delete multiple lines or connectors, select the first line, press and hold Ctrl while you select the other lines, and then press Delete.

How do I delete all tabs except one in Excel?

Click Kutools Plus > Worksheet > Delete All Inactive Sheets. 3. Then a dialog box pops up, if you determine to delete all sheets except active one, click the OK button, otherwise, click the Cancel button. Then all sheets are deleted except the current active one in your workbook.


1 Answers

To delete autoshapes and textboxes only you can use:

Sub DeleteAllShapes()

Dim Shp As Shape

For Each Shp In ActiveSheet.Shapes
    If Shp.Type = msoAutoShape Or Shp.Type = msoTextBox Then Shp.Delete
Next Shp

End Sub

Alternatively you can work the other way around and specify the types not to delete. You can use the enumerated types but it's more readable to use the type names. The following snippet will delete everything apart from Form Controls and OLE control objects.

Sub DeleteAllShapes()

Dim Shp As Shape

For Each Shp In ActiveSheet.Shapes
    If Not (Shp.Type = msoOLEControlObject Or Shp.Type = msoFormControl) Then Shp.Delete
Next Shp

End Sub

A full list of MSO Shape Types. http://msdn.microsoft.com/en-us/library/office/aa432678(v=office.12).aspx

Ron de Bruin has a good collection of snippets which may be relevant to anyone else coming across this question. http://www.rondebruin.nl/controlsobjectsworksheet.htm

like image 97
Jamie Bull Avatar answered Oct 21 '22 07:10

Jamie Bull