logo
search
VBA & Macro Problems

Excel VBA Macro to Delete Shapes Within a Selected Range

Muhammad TalhaMuhammad Talha Oct 8, 2026 872 views

Question details

The user needs an Excel VBA macro script that can identify and delete shapes or objects specifically located within their currently selected worksheet range.

How to Use an Excel VBA Macro to Delete Shapes Within a Selected Range
Product
Excel
Device & OS
not provided
Scenario
Removing objects from a defined worksheet area without accidentally deleting shapes, charts, or images located elsewhere on the same sheet.
Observed behavior
A VBA script is required to loop through all shapes and evaluate if their top-left anchoring cell intersects with the user's active selection before executing the delete command.
Before you start

Before running any VBA macro to delete objects, ensure you save a backup copy of your workbook. Actions executed via macros clear your undo history, meaning deleted shapes cannot be recovered using Ctrl + Z.

Solution 1Recommended

Use a VBA Macro to Check Intersection and Delete Shapes

Execute a VBA script that loops through all shapes on the active worksheet, checks if a shape's top-left cell intersects with your current selection, and deletes it if true.

This solution targets shapes based on their top-left cell anchor. It is highly efficient for clearing out a specific section of a worksheet while preserving design elements like logos or buttons positioned outside the highlighted area.

1
Select the target range

Open your worksheet and highlight the specific cell range where you want shapes to be removed.

2
Open the Visual Basic Editor

Press the Alt + F11 keys on your keyboard simultaneously to launch the Visual Basic for Applications (VBA) editor.

3
Insert a new module

In the VBE top menu, click on 'Insert' and select 'Module' from the drop-down list to create a blank workspace for your code.

4
Paste the VBA script

Copy and paste the following code into the module window: Sub DeleteShapesInRange() Dim shp As Shape Dim rng As Range Dim ws As Worksheet Set ws = ActiveSheet Set rng = Selection For Each shp In ws.Shapes If Not Intersect(shp.TopLeftCell, rng) Is Nothing Then shp.Delete Next shp End Sub

5
Run the macro

Close the Visual Basic Editor. Back in your worksheet, press Alt + F8 to open the Macro dialog box, select 'DeleteShapesInRange' from the list, and click 'Run'.

Use a VBA Macro to Check Intersection and Delete Shapes
Shape Positioning Criterion: This macro relies exclusively on the TopLeftCell property. If a shape visually overlaps your selected range but its top-left corner is anchored in a cell outside the selection, it will not be deleted.

Run VBA Macros Seamlessly in WPS Spreadsheets

WPS Office provides exceptional compatibility with Microsoft Excel macros and VBA scripts. You can quickly execute shape-deleting scripts and manage your spreadsheet objects using the built-in Developer tools.

  1. 1. Select your cell range: Open your workbook in WPS Spreadsheets and highlight the specific cell area containing the shapes you want to delete.
  2. 2. Access the Developer tab: Navigate to the 'Developer' tab on the top ribbon and click on 'Visual Basic Editor', or simply press Alt + F11.
  3. 3. Insert and paste code: Go to Insert > Module in the editor, and paste the shape-deleting VBA script provided into the code window.
  4. 4. Execute the macro: Close the editor, click 'Macros' (Alt + F8) from the Developer tab, choose your new macro, and click 'Run'.
High compatibility with Microsoft Excel (.xlsm) macro-enabled filesFamiliar Developer tab interface for writing and testing VBA codeLightweight application that runs smoothly on most operating systemsFree to download with comprehensive, professional spreadsheet tools
microsoft office alternative - wps office

Frequently Asked Questions

Why didn't the macro delete a shape that is visibly inside my selected area?

The macro evaluates the shape's TopLeftCell property. If a shape stretches into your selection but its extreme top-left corner is anchored in a cell outside your highlighted range, the macro will skip it.

Can I modify the macro to ensure the entire shape is within the range?

Yes. You can update the script to check both the TopLeftCell and BottomRightCell properties. Both points must intersect with your selected range to trigger the deletion.

Is it possible to undo the deletion of shapes made by a VBA macro?

Generally, actions performed by VBA macros clear the application's undo stack, meaning you cannot use Ctrl + Z to restore the deleted shapes. This is why saving a backup copy of your workbook before running the macro is highly recommended.

How do I delete all shapes on the entire worksheet instead of just a selected range?

To delete all shapes on the active worksheet, you can use a simpler VBA script: ActiveSheet.Shapes.SelectAll followed by Selection.Delete, or run a loop that deletes each shape without checking for an intersection.