vba 获取 Excel 图表 Y 轴的最大值(当有两个时)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3065009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting The Maximum Of An Excel Chart's Y Axis (When There Are Two)
提问by sooprise
I would like to know how to programmatically find and the Y axis maximum of an excel chart when there is more than one available.
我想知道当有多个可用图表时,如何以编程方式查找 Excel 图表的 Y 轴最大值。
My end goal is to find the max y-axis values, compare them, and set them both to the greater of the two.
我的最终目标是找到最大 y 轴值,比较它们,并将它们设置为两者中的较大者。
回答by Cameron McGrane
VBA similar to this will retrieve scale value
与此类似的 VBA 将检索比例值
With ActiveChart.Axes(xlValue, xlPrimary)
ActiveSheet.Range("A1").Value = .MaximumScale
End With
For more detail on how to link chart axis scale parameters to values in cells check out
有关如何将图表轴刻度参数链接到单元格中的值的更多详细信息,请查看
http://peltiertech.com/Excel/Charts/AxisScaleLinkToSheet.html#ixzz0r8qN248l
http://peltiertech.com/Excel/Charts/AxisScaleLinkToSheet.html#ixzz0r8qN248l
回答by Jon Peltier
If you want both axes to automatically show the same min and max values, you need both to plot the same spread of data.
如果您希望两个轴自动显示相同的最小值和最大值,则需要两者都绘制相同的数据分布。
In this simple example, I used the following data to create a chart, with "primary" plotted on the primary axis and "secondary" plotted, well, you know.
在这个简单的示例中,我使用以下数据创建了一个图表,“主要”绘制在主轴上,“次要”绘制,你知道的。
In the next table, I've calculated the min and max of all the data. I calculated min and max twice and staggered them to clearly show what I'm doing. I copied the shaded range, celected the chart, and used Paste Special to add the data as new series, in columns, series names in first row.
在下表中,我计算了所有数据的最小值和最大值。我计算了 min 和 max 两次并将它们交错以清楚地显示我在做什么。我复制了阴影范围,选择了图表,并使用选择性粘贴将数据添加为新系列,列中,第一行中的系列名称。
The resulting chart is shown below left. I've ensured the the new series "pri" is on the primary axis and "sec" is on the secondary axis. Since both axes are using the same min and max data to autoscale, both have the same scales. Below right I've hidden the dummy series by formatting them with no lines and no markers. I've hidden each unwanted legend entry by first clicking on the legend, then clicking on the individual legend entry, then pressing Delete.
结果图表显示在左下方。我确保新系列“pri”在主轴上,“sec”在次轴上。由于两个轴都使用相同的最小和最大数据进行自动缩放,因此它们具有相同的比例。在右下方,我通过将它们格式化为没有线条和标记来隐藏虚拟系列。我通过首先单击图例,然后单击单个图例条目,然后按 Delete 来隐藏每个不需要的图例条目。
This technique can also be used to synchronize the axes of multiple charts. Below are three small charts with different scales (top row). In the middle row, the calculated min and max have been added to each chart, causing their axes to autoscale on the same min and max. In the bottom row, the dummy series have been hidden, leaving behind uniform axis scales.
此技术还可用于同步多个图表的轴。下面是三个不同比例的小图表(顶行)。在中间一行,计算的最小值和最大值已添加到每个图表中,导致它们的轴在相同的最小值和最大值上自动缩放。在底行中,虚拟系列已被隐藏,留下统一的轴刻度。