问题
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