VBA to add string after data labels in pie chart

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

VBA to add string after data labels in pie chart

Post by Rudi »

Hi,

The code to add a data label to the series is as follow:

Code: Select all

ActiveChart.SeriesCollection(1).ApplyDataLabels AutoText:=True, LegendKey:= _
        False, HasLeaderLines:=True, ShowSeriesName:=False, ShowCategoryName:= _
        False, ShowValue:=True
Is there a way to add a string behind the value?
I foresee it will need to be looped?

TX
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78446
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA to add string after data labels in pie chart

Post by HansV »

Do you want to add a fixed string? If so, you may be able to set the NumberFormat property of the DataLabels object to display the string.
Or does the string vary from data point to data point?
Best wishes,
Hans

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: VBA to add string after data labels in pie chart

Post by Rudi »

It is a fixed string yes; simply "AAA"
So a data label of value 123 would look like this "123 AAA"

Can a number format property take a string?
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78446
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA to add string after data labels in pie chart

Post by HansV »

Yes; you have to enclose the string in double double quotes, like this:

Code: Select all

ActiveChart.SeriesCollection(1).DataLabels.NumberFormat = "0 ""AAA"""
Best wishes,
Hans

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: VBA to add string after data labels in pie chart

Post by Rudi »

Many TX :)
I'll test it out.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: VBA to add string after data labels in pie chart

Post by Rudi »

Quick feedback...
The code with your addition worked great. :chocciebar:
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78446
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA to add string after data labels in pie chart

Post by HansV »

Thanks! :yum:
Best wishes,
Hans