简体   繁体   English

VBA更改图表中的数据标签-兼容btw Excel 2013和2010

[英]VBA to change data label in a chart - Compatibility btw Excel 2013 and 2010

I did a macro to change the number format of a chart's label in a dashbord, because the values can be either thousands or millions. 我做了一个宏来更改仪表板中图表标签的数字格式,因为值可以是数千或数百万。 In my code, I already changed the object FullSeriesCollection that exists only in Excel 2013 and later to SeriesCollection. 在我的代码中,我已经将仅在Excel 2013和更高版本中存在的对象FullSeriesCollection更改为SeriesCollection。 However I am still facing issues when I run the code in Excel 2010, because the custom number format code brings completely different result. 但是,当我在Excel 2010中运行代码时,仍然遇到问题,因为自定义数字格式代码带来了完全不同的结果。

Follows the VBA for millions 跟随VBA数百万人

    Sheets("Gráficos").ChartObjects("n2_resultado").Activate
ActiveChart.SeriesCollection(1).DataLabels.Select
    Selection.NumberFormat = "#.##0,0..;-#.##0,0..;"""""
ActiveChart.SeriesCollection(2).DataLabels.Select
    Selection.NumberFormat = "#.##0,0..;-#.##0,0..;"""""
ActiveChart.SeriesCollection(3).DataLabels.Select
    Selection.NumberFormat = "#.##0,0..;-#.##0,0..;"""""
ActiveChart.SeriesCollection(4).DataLabels.Select
    Selection.NumberFormat = "#.##0,0..;-#.##0,0..;"""""
ActiveChart.SeriesCollection(5).DataLabels.Select
    Selection.NumberFormat = "#.##0,0..;-#.##0,0..;"""""
ActiveChart.SeriesCollection(6).DataLabels.Select
    Selection.NumberFormat = "#.##0,0..;-#.##0,0..;"""""

For thousands values, the number format code is this: 对于数千个值,数字格式代码是这样的:

"#.##0,0.;-#.##0,0.;"""""

I don't have easily access to a Excel 2010, but in a glimpse yesterday it seemed the code we use in the front-of-the-house doesn't work in the back-of-the-house. 我无法轻松访问Excel 2010,但昨天一瞥,似乎我们在屋前使用的代码在屋后无法使用。

Any ideia how can I solve it? 任何想法我该如何解决?

Note: "Gráficos" is the word for "Chart" in Portuguese and in Brazil we use a dot as a thousands separator and a comma for decimals (the oposite of most of the world lol) 注意:“Gráficos”是葡萄牙语中的“图表”一词,在巴西,我们使用点作为千位分隔符,并使用逗号表示小数点(世界上大多数情况相反)

This is the messy chart I got 这是我得到的凌乱图表

It seems that Excel 2010 and 2013 work differently regarding to number format code in Macros. 对于宏中的数字格式代码,Excel 2010和2013的工作方式似乎有所不同。 Due my limited access to Excel 2010, the solution was to duplicate the charts, change the labels as necessary and then switch the macro to show only the charts with the millions/thousands labels. 由于我对Excel 2010的访问受限,解决方案是复制图表,根据需要更改标签,然后切换宏以仅显示带有百万/千标签的图表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM