I am trying to use VBA to add datalabels to a graph.
I have this code that is working very well to add datalabeles to all the series that I have in the graph.
Sub Apply_Data_Labels()
'Applies data labels to all
'data series on the set chart
'Set number format of data labels
Sheets("Grafico Pivot").Unprotect
Const NumFormat = "#.##0"
Dim Cht As Chart
Dim Ser As Series
'Set the chart
Set Cht = Sheets("Grafico Pivot").ChartObjects("Chart 1").Chart
'Apply data lables
For Each Ser In Cht.SeriesCollection
Ser.ApplyDataLabels
Ser.DataLabels.NumberFormat = NumFormat
Ser.DataLabels.Font.Size = 10
Next Ser
Sheets("Grafico Pivot").Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End Sub
Now I have a new challenge. I need to create a Graph where the datalabel can have only a limited number of values, from 1 to 20. For each number I need to display a particular text code (as in the attached image)
How can I do this?
I tried to use the If cause but I was not able to implement the right code.
Niccolo Rivato is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.