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.
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"
Sheets("hydro vs asperity"
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.