VB CODE TO CHANGE DATALABEL

  • Thread starter Thread starter Mike Waldron
  • Start date Start date
M

Mike Waldron

Hello,

Would someone please tell the VB code to change the
datalable for a point in a series on a MS Graph chart.
I'm running the code from Excel and trying to update an MS
Graph chart in PowerPoint. I can open PowerPoint and
Identify the chart. From that point on I'm lost.

Thanks in advance

Mike
 
Mike,
' ------------------------- Code Block -------------------------
'Set a reference the graph library else declare oChart as Object

Sub DataLabels()
Dim oChart As Graph.Chart
Dim x As Integer, y As Integer

' Set the reference to the Chart object.
Set oChart = ActivePresentation.Slides(1).Shapes(1).OLEFormat.Object
' Iterate thru each series
For x = 1 To oChart.SeriesCollection.Count
' And then thru each data point in the series
For y = 1 To oChart.SeriesCollection(x).Points.Count
With oChart.SeriesCollection(x)
' If the datapoint has a label...
If .Points(y).HasDataLabel Then
' Put the text to be replaced here. I am just displaying the exisiting text.
Debug.Print .Points(y).DataLabel.Text
End If
End With
Next y
Next x
End Sub
' ------------------End of Code Block -----------------------
 
Mike,
Include the 2 lines added at the bottom to ensure that the chart saves any
changes you make to the datalabels.
' ------------------------- Code Block -------------------------
'Set a reference the graph library else declare oChart as Object
Sub DataLabels()
Dim oChart As Graph.Chart
Dim x As Integer, y As Integer

' Set the reference to the Chart object.
Set oChart = ActivePresentation.Slides(1).Shapes(1).OLEFormat.Object

' Iterate thru each series
For x = 1 To oChart.SeriesCollection.Count
' And then thru each data point in the series
For y = 1 To oChart.SeriesCollection(x).Points.Count
With oChart.SeriesCollection(x)
' If the datapoint has a label...
If .Points(y).HasDataLabel Then
' Put the text to be replaced here. I am just displaying the exisiting text.
Debug.Print .Points(y).DataLabel.Text
End If
End With
Next y
Next x
oChart.Application.Update ' New line of code
oChart.Application.Quit ' New line of code
End Sub
' ----------------------End of Code Block ----------------------
 
Back
Top