It makes sense to name different things differently. Problems may arise when this isn’t the case. I found this out when two images ended up with the same name.
Belaboring the default behavior
The default behavior when inserting a shape is to provide it a unique name. Anything inserted onto a worksheet is considered a Shape object. Test the default behavior by inserting a few shapes from the ribbon under Insert>Illustrations group>Shapes. After you do place the cursor over the cells and it will be a small + sign. Left click and hold the button dragging to select an area, releasing the button to create the shape.
When the shape is created it will also be selected. When a shape is selected you can see its name in the Name Box.
Insert an identical shape and the numeric identifier for the new shape is incremented, giving it a unique name.
Below I’ve copied Rectangle 2. When copied the name for the 3rd shape is automatically assigned Rectangle 3.
Getting a duplicate name
And yet…. this doesn’t occur when you change the name from the default. I’ve renamed Rectangle 3 to BlueVerticalRectangle via the Name Box.
When the renamed shape is copied the name isn’t updated.
Confirm this via the Home tab>Editing Group>Find & Select menu>Selection Pane (Hotkey: Alt+F10
). Below you see 2 shapes that both have the name BlueVerticalRectangle.
Problems with identical names
The problem I encountered occurred when needing to toggle a shapes visibility. To simplify this example I:
- Created a Named Range ShapeVisibilityToggle in cell B8.
- Applied Data Validation to this cell to only allow True/False values to be selected.
- Applied the Input cell style to easily identify it.
I created a Sub/Macro to utilize the value it contains.
Public Sub ToggleShapeVisibility()
Dim displayShape As Boolean
displayShape = Sheet1.Range("ShapeVisibilityToggle").Value
Sheet1.Shapes("Rectangle 1").Visible = displayShape
Sheet1.Shapes("Rectangle 2").Visible = Not displayShape
Sheet1.Shapes("BlueVerticalRectangle").Visible = displayShape
End Sub
I added a button to the worksheet to run the Macro.
With the set up done shape visibility is now easily toggled by clicking the button. If you’ve been following along try with both values of True and False. The rightmost shape is always displayed.
Nit-picking-probably-shouldn’t-include details
“Technically” the rightmost shape can be toggled off. By changing its order in the Shapes collection of the Worksheet.Shapes property it’s on. You can do this by clicking and holding the left mouse button when selecting an item in the Selection Pane then dragging it to the bottom.
Once done the list will be as below. Update the visibility by changing the value and seeing that the 3rd shapes visibility never changes. All we did was change which image can be toggled.
The moral of all this? Ensure there are unique names for any shapes.
Check for distinct shape names
Public Function HasDistinctShapeNames(ByVal ws As Worksheet) As Boolean
If ws.Shapes.Count = 0 Then
HasDistinctShapeNames = True
Exit Function
End If
Dim dict As Object 'Scripting.Dictionary
Set dict = CreateObject("Scripting.Dictionary") 'New Dictionary
Dim checkShape As Shape
For Each checkShape In ws.Shapes
If dict.Exists(checkShape.Name) Then
HasDistinctShapeNames = False
Exit Function
Else
dict.Add checkShape.Name, checkShape.Name
End If
Next
HasDistinctShapeNames = True
End Function
A quick-n-dirty check for shape name distinctness. Late binding used to eliminate the need for adding the reference to Microsoft Scripting Runtime.