Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

What to do when chart has more than one name?

Status
Not open for further replies.

ivymike

Mechanical
Nov 9, 2000
5,653
I've recently come across a problem when trying to resize a selected chart via a macro:

Activechart.name returns a different value than what would be used in a reference such as Sheets(1).ChartObjects(MyChartName)

When I run the following test code

Sub test1()
Debug.Print "-----------------------"
Debug.Print Sheets("hydro vs asperity").ChartObjects(1).Name
Sheets("hydro vs asperity").ChartObjects(1).Activate
Debug.Print ActiveChart.Name
End Sub

it returns
-----------------------
Chart 1
Hydro vs Asperity Chart 1

So activechart.name is different than chartobjects(1).name, but in a predictable manner.

I'd like to be able to programmatically resize a selected chart. The trouble is that if I want to use a command such as this:

Sheets(MySheetName).Shapes(MyChartName).ScaleWidth (NewWidth / LastWidth), msoFalse, msoScaleFromTopLeft

my code needs to have the right name - "Chart 1" rather than the sheetname + "Chart 1"

The workaround that I came up with is to try to match up activechart.name to chartobjects(1).name as follows:

MySheetName = ActiveSheet.Name
MyChartName = ActiveChart.Name
For Each MyChartObject In ActiveSheet.ChartObjects
If MyChartName Like ("*" + MyChartObject.Name) Then
MyChartName = MyChartObject.Name
End If
Next

I'd really like to know of a more graceful way to accomplish the same.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor