I am wondering now how I can change tree_10 to another name.
(or any tree name) This is another exercise in itself.
Sub Test()
Dim nResult As Long
nResult = RenameShape("Tree_10", "Tree_20", ActiveSheet)
If nResult = 1 Then
MsgBox "A shape already exists with new name"
ElseIf nResult = 2 Then
MsgBox "No shape exists with old name"
End If
End Sub
Function RenameShape(sOld As String, sNew As String, _
Optional ws As Worksheet) As Long
Dim shp As Shape
If ws Is Nothing Then
Set ws = ActiveSheet ' assumes not a chart sheet
End If
On Error Resume Next
'ensure no shape exists with new name
'and a shape does exist with old name
Set shp = ws.Shapes(sNew)
If Not shp Is Nothing Then
RenameShape = 1
Else
Set shp = ws.Shapes(sOld)
If Not shp Is Nothing Then
shp.Name = sNew
Else
RenameShape = 2
End If
End If
End Function
Regards,
Peter T