How to rename the selected shape in Excel

风格不统一 提交于 2020-07-08 03:18:11

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/24305334/how-to-rename-the-selected-shape-in-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!