问题
I ran into a wall setting minimum- and maximumscale properties of a chart's secondary axis - kept getting an "object does not have this property" error. I fixed it, but I'd like to know how.
After too many hours of googling and trying different options (Three. Three stupid hours), I arbitrarily decided to reference the chart by way of the Shapes collection instead of the ChartObjects collection, and that worked.
So, good:
ActiveSheet.Shapes(1).Chart.Axes(xlValue,xlSecondary).MinimumScale = cMin
and bad:
ActiveSheet.ChartObjects(1).Chart.Axes(xlValue,xlSecondary).MinimumScale = cMin
but I was able to set other properties. E.g., the following line of code worked:
ActiveSheet.ChartObjects(1).Chart.Axes(xlValue,xlSecondary).AxisTitle.Text = "Current (A)"
From the MSDN documentation, both Shapes(1) and ChartObjects(1) return a Chart object, yet I couldn't access all properties when I accessed the Chart via the ChartObjects collection. That is, I am able to set all properties within the Axes method when I set the chart as
Dim cht as Chart
Set cht = ActiveSheet.Shapes(1).Chart
but not if I set the chart as
Dim cht as Chart
Set cht = ActiveSheet.ChartObjects(1).Chart
So my question is, why this discrepancy? Is this some hidden nuance of the VBA syntax, or more likely a foible of my machine?
Excel 2013 on a Windows 7 box
来源:https://stackoverflow.com/questions/30940387/excel-referencing-a-chart-as-chartobjects-chart-vs-shapes