Excel: referencing a chart as ChartObjects & Chart vs Shapes

こ雲淡風輕ζ 提交于 2019-12-12 21:57:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!