I am trying to set the values on markers in an Excel sheet, using a custom-range.
I am using a script, which is run on the Workbook_Open event.
The code runs, however the values from the custom-range are not displayed on the markers.
No values are displayed at all.
My VBA code is:
' The open event.
Private Sub Workbook_Open()
' Add a chart.
ActiveSheet.Shapes.AddChart2(240, xlXYScatter).Select
' Give the chart a name.
Dim ch As Shape
Set ch = ActiveSheet.Shapes(1)
ch.Name = "ScatterChart"
' Set the chart range etc.
ActiveChart.SetSourceData Source:=Range("'MySheet'!$C$1:$D$12")
ActiveChart.SetElement (msoElementDataLabelCenter)
ActiveChart.FullSeriesCollection(1).Select
' Add some styling for the markers.
With Selection
.MarkerStyle = 8
.MarkerSize = 15
.Format.Fill.Visible = msoFalse
.MarkerForegroundColor = vbBlack
End With
' *** Try to set the values on the markers. (Issue is somewhere in here.) ***
ActiveChart.SeriesCollection(1).ApplyDataLabels
ActiveChart.FullSeriesCollection(1).DataLabels.Select
ActiveChart.SeriesCollection(1).DataLabels.Format.TextFrame2.TextRange.InsertChartField msoChartFieldRange, "='MySheet'!$B$1:$B$12", 0
Selection.ShowRange = True
Selection.ShowValue = False
Selection.Position = xlLabelPositionCenter
' Shift focus to the first cell.
Range("A1").Select
End Sub
Thank you for any assistance.