Using Names instead of Shape Index Numbers

paisiello

New Member
Joined
Jun 14, 2012
Messages
7
I am having a hard time figuring out how to make this work so hopefully someone can assist me.

1) When I try the following code in my macro it works fine:

ActiveSheet.Shapes(1).Select
Selection.Delete

But when I substitue a name like this:

ActiveSheet.Shapes("Shape_Name").Select
Selection.Delete

I get "The item with the specified name doesn't exist" error message.

2) When I go to the name manager it shows that "Shape_Name" has the Value of "Freeform 1362" and Refers To "=Freeform 1362". When I click on the object it shows as "Freeform 1362" in the name window.

What am I missing? I am writing this as a VBA Module and want to refer to all shapes by the name I give them.

TIA
 
Maybe ...

Code:
    Dim shp         As Shape

    On Error Resume Next
    Set shp = Worksheets("Directions").Shapes("Picture 1")
    If shp Is Nothing Then
        ' it doesn't exist
    End If
 
Upvote 0

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Trying to delete a non-existing object gives a 400 error. What is the syntax for checking if Sheets("Directions").Shapes.Range(Array("Picture 1")) exists, before deleting it? I'm still new to IF statements.

___________________
[WIT] Remarks [/WIT]

There are two ways that I was able to find out. The first method checks the name directly and resumes the code if an error occurs:

On Error Resume Next
If Not IsEmpty(ActiveSheet.Shapes("Shape_Name").ID) Then ActiveSheet.Shapes("Shape_Name").Delete


The second method loops through all the shapes and checks to see if any of them correspond to the specified name or if any portion of them correspond to the specified name:

Dim Shp As Shape
For Each Shp In ActiveSheet.Shapes
If Not InStr(Shp.Name, "Shape_Name") = 0 Then
Shp.Delete
End If
Next Shp

I can't get either of the two methods to work, however, when using a variable name for "Shape_Name". Even using [] doesn't help. Maybe shg has a suggestion here?
 
Upvote 0
On Error Resume Next
If Not IsEmpty(ActiveSheet.Shapes("Shape_Name").ID) Then ActiveSheet.Shapes("Shape_Name").Delete

Using the "Picture 1" etc. names worked well for me, thanks. And I've since taken some of this and applied it to other conditionals (if a cell is blank on code execution, display messagebox and exit sub)

The problem with renaming shapes, is that you can name multiple pictures with the same name, and I can't find a unique identifier on the similarly named shapes...
 
Upvote 0

Forum statistics

Threads
1,214,793
Messages
6,121,619
Members
449,039
Latest member
Mbone Mathonsi

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