PhilHibbs
PhilHibbs

Reputation: 943

How to rename the selected shape in Excel

If I select a shape, such as a chart or text box, how can I rename it in VBA? I have a nice little sub and form in PowerPoint that does this:

Sub ShapeName()
If Not ActiveWindow.Selection Is Nothing Then
    If ActiveWindow.Selection.Type = ppSelectionShapes Then
        NameForm.NameBox.Text = ActiveWindow.Selection.ShapeRange.Name
        NameForm.Show
        If Not NameForm.bCancel Then
            ActiveWindow.Selection.ShapeRange.Name = NameForm.NameBox.Text
        End If
    End If
End If
End Sub

How can I achieve the same in Excel? The ActiveWindow.Selection object is very different. I can't work out how to navigat from the selection to the selected shape. If the shape selected is a chart object, then the source cells are also selected, and I want to ignore those and just rename the shape.

Upvotes: 0

Views: 42855

Answers (1)

Gary's Student
Gary's Student

Reputation: 96781

Here is a small example:

Sub ARoseByAnyOtherName()
    ActiveSheet.Shapes(1).Select
    Selection.Name = "MyRedRose"
End Sub

EDIT#1:

If we know that the Selected object is a Chart then use:

Sub ARoseByAnyOtherName()
    ActiveChart.Parent.Name = "MyRedRose"
End Sub

Upvotes: 7

Related Questions