VBA to delete ALL objects EXCEPT the following...

KP_SoCal

Board Regular
Joined
Nov 17, 2009
Messages
116
I'm running the sub routine below to delete all shape objects on an active worksheet. I'd like to modify this route to delete all objects except "shapeButton1" and "shapeOption25". Any suggestions?


Code:
Sub DeletesShapes()
'Delete all Objects except Comments
    On Error Resume Next
    ActiveSheet.DrawingObjects.Visible = True
    ActiveSheet.DrawingObjects.Delete
    On Error GoTo 0
End Sub

P.S. I'm using Excel 2010
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Maybe ...

Code:
Sub DeletesShapes()
    Dim o As Object
    

    For Each o In ActiveSheet.DrawingObjects
        If o.Name <> "shapeButton1" And o.Name <> "shapeOption25" Then
            o.Delete
        End If
    Next o
    On Error GoTo 0
End Sub

EDIT: The comparison is case-sensitive as written.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,759
Messages
6,126,732
Members
449,333
Latest member
Adiadidas

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top