Excel, just like other Office products, supports AutoShapes. Although Excel shapes are nice to have for presentation purposes, getting rid of them may become time consuming if your workbook is cluttered with them. This article shows you how to delete Excel shapes (AutoShapes) automatically using VBA.

 

How to delete Excel shapes (AutoShapes) using VBA

AutoShapes are stored in the Shapes collection under a worksheet object. This means that we can loop through Excel shapes in a worksheet and delete them.

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

To delete the AutoShapes from the whole workbook, we need to check every sheet.

    For Each ws In ActiveWorkbook.Worksheets
        For Each shp In ws.Shapes
            shp.Delete
        Next shp
    Next ws

You can use codes in two ways:

  • Module
  • Immediate Window

In the Module method, you need to add the module into the workbook or the add-in file. Copy and paste the code into the module to run it. The main advantage of the module method is that it allows saving the code in the file, so that it can be used again later. Furthermore, the subroutines in modules can be used by icons in the menu ribbons or keyboard shortcuts. Remember to save your file in either XLSM or XLAM format to save your VBA code.

The Immediate Window method, on the other hand, is essentially a quick and dirty method where you can simply copy and paste the code into the Immediate Window and press the Enter key to run it. Unfortunately, any code you use in the Immediate Window will not be saved. Also note that icons and keyboard shortcuts will not be available.

 

Delete AutoShapes in an active sheet

Module method:

Sub DeleteShapesInActiveWorksheet()

    Dim shp As Shape

    For Each shp In ActiveSheet.Shapes

        shp.Delete

    Next shp

End Sub

Immediate Window method:

For Each shp In ActiveSheet.Shapes: shp.Delete: Next shp

 

Delete AutoShapes in a workbook

Module method:

Sub DeleteShapesInActiveWorkbook()
    Dim ws As Worksheet
    Dim shp As Shape
    For Each ws In ActiveWorkbook.Worksheets
        For Each shp In ws.Shapes
            shp.Delete
        Next shp
    Next ws
End Sub

Immediate Window method:

For Each ws In ActiveWorkbook.Worksheets: For Each shp In ws.Shapes: shp.Delete: Next shp: Next ws