Excel VBA Macro to Delete Shapes Within a Selected Range
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.

- 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 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.
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.
Open your worksheet and highlight the specific cell range where you want shapes to be removed.
Press the Alt + F11 keys on your keyboard simultaneously to launch the Visual Basic for Applications (VBA) editor.
In the VBE top menu, click on 'Insert' and select 'Module' from the drop-down list to create a blank workspace for your code.
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
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'.

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. Select your cell range: Open your workbook in WPS Spreadsheets and highlight the specific cell area containing the shapes you want to delete.
- 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. Insert and paste code: Go to Insert > Module in the editor, and paste the shape-deleting VBA script provided into the code window.
- 4. Execute the macro: Close the editor, click 'Macros' (Alt + F8) from the Developer tab, choose your new macro, and click 'Run'.

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.




