Saturday 28 January 2017

change label to percentage in bar graph excel using vba macro

Sub set_data_labels_to_bar_chart1()

Dim i As Integer
Dim j As Integer
Dim s As Double
Dim v As Variant

Dim NoDigits As Integer
Dim PercentFormat As String
Dim myTxt As String

NoDigits = 1 'How many digits to round Millions toPercentFormat = "0.0%" 'Format string for the Percentages
With ActiveChart
  For i = 1 To .SeriesCollection.Count
  .SeriesCollection(i).HasDataLabels = True
  Next i
 
  For i = 1 To .SeriesCollection(1).Points.Count
  s = 0
   
  For j = 1 To .SeriesCollection.Count
  v = .SeriesCollection(j).Values
  s = s + v(i)
  Next j
   
  For j = 1 To .SeriesCollection.Count
  v = .SeriesCollection(j).Values
  myTxt = Round(v(i) / 1000000#, NoDigits) & "M, " & Format(v(i) / s, PercentFormat)
  .SeriesCollection(j).Points(i).DataLabel.Text = myTxt
   
  If v(i) <= 0 Then .SeriesCollection(j).Points(i).DataLabel.Delete
  Next j
  Next i
End With
End Sub

No comments:

Post a Comment