Selection - ShapeRange and chart issue

excel, vba

Solution

Is this what you are trying?

If TypeName(Selection) = "ChartArea" Then
    Debug.Print ActiveChart.Parent.Name
Else
    Debug.Print Selection.ShapeRange(1).Name
End If

I am assuming that a `Shape` is selected when you run this code.

Edit

Or you can use `Selection.Parent.Parent.Name` instead of `ActiveChart.Parent.Name`

In school, while learning Geometry, I learned that a `Square` can be a `Rectangle` but a `Rectangle` can not be called a `Square`. Both are Geometric shapes yet they have different properties. Similarly, a rectangular shape and a chart are shapes but it is not necessary that they will share the same properties. Another example would be a `Range` is an `Object` but so is a `Shape`. But then you cannot weigh them in the same scale. They have to be addressed as separate objects

The above code can be further narrowed down to

Select Case TypeName(Selection)
    Case "ChartArea": Debug.Print Selection.Parent.Parent.Name
    Case "Range": Debug.Print Selection.Address
    Case Else: Debug.Print Selection.ShapeRange(1).Name
End Select

Problem

I've found a problem with obtaining name properties from my hand made selection. I use very simple code to get selected shapes names: ``` Selection.ShapeRange(i).Name ``` Everything works fine and I get names of autoshapes, smartarts, charts. The problem shows up when I have only one object selected and the object is a chart. I try to execute my code (or try any method else for example some simple: top, left and other methods) and I am getting such error: ``` Run-time error "438": Object doesn't support this propert or method ``` I don't understand it. Is the possible list of methods different for charts when they are selected alone than when selected with other charts/shapes?

Original source