vba 如何在 Excel 中重命名选定的形状

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24305334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:21:09  来源:igfitidea点击:

How to rename the selected shape in Excel

vbaexcel-vbaexcel

提问by PhilHibbs

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:

如果我选择一个形状,例如图表或文本框,如何在 VBA 中重命名它?我在 PowerPoint 中有一个不错的小子和表单,可以执行此操作:

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.

如何在 Excel 中实现相同的目标?ActiveWindow.Selection 对象非常不同。我不知道如何从选择导航到选定的形状。如果选择的形状是图表对象,那么源单元格也被选中,我想忽略这些并重命名形状。

回答by Gary's Student

Here is a small example:

这是一个小例子:

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

EDIT#1:

编辑#1:

If we know that the Selectedobject is a Chartthen use:

如果我们知道Selected对象是一个Chart则使用:

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