Search code examples
excelvbaexcel-2010excel-2013

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. However I am still facing issues when I run the code in Excel 2010, because the custom number format code brings completely different result.

Follows the VBA for millions

    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.

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)

This is the messy chart I got


Solution

  • It seems that Excel 2010 and 2013 work differently regarding to number format code in Macros. 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.