I'm really a beginner with VBA macros and their syntax so bear with me please. I have been trying to assign content from specific cells to labels in a scatter plot. Started out by recording my own manual assignment and "cleaning" that code. Works fine for the sample set I'm using right now.
Sub Rename_scatter()
'
' Rename_scatter Macro
'
'
ActiveSheet.ChartObjects("Risikomatrix").Activate
ActiveChart.FullSeriesCollection(1).DataLabels.Select
ActiveChart.FullSeriesCollection(1).Points(1).DataLabel.Select
ActiveChart.SeriesCollection(1).DataLabels(1).Format.TextFrame2.TextRange. _
Characters.Text = ""
ActiveChart.SeriesCollection(1).DataLabels(1).Format.TextFrame2.TextRange. _
InsertChartField msoChartFieldFormula, "='Risiko-Log'!$B$2", 1
ActiveChart.FullSeriesCollection(1).Points(2).DataLabel.Select
ActiveChart.SeriesCollection(1).DataLabels(2).Format.TextFrame2.TextRange. _
Characters.Text = ""
ActiveChart.SeriesCollection(1).DataLabels(2).Format.TextFrame2.TextRange. _
InsertChartField msoChartFieldFormula, "='Risiko-Log'!$B$3", 1
ActiveChart.FullSeriesCollection(1).Points(3).DataLabel.Select
ActiveChart.SeriesCollection(1).DataLabels(3).Format.TextFrame2.TextRange. _
Characters.Text = ""
ActiveChart.SeriesCollection(1).DataLabels(3).Format.TextFrame2.TextRange. _
InsertChartField msoChartFieldFormula, "='Risiko-Log'!$B$4", 1
ActiveChart.FullSeriesCollection(1).Points(4).DataLabel.Select
ActiveChart.SeriesCollection(1).DataLabels(4).Format.TextFrame2.TextRange. _
Characters.Text = ""
ActiveChart.SeriesCollection(1).DataLabels(4).Format.TextFrame2.TextRange. _
InsertChartField msoChartFieldFormula, "='Risiko-Log'!$B$5", 1
ActiveChart.FullSeriesCollection(1).Points(5).DataLabel.Select
ActiveChart.SeriesCollection(1).DataLabels(5).Format.TextFrame2.TextRange. _
Characters.Text = ""
ActiveChart.SeriesCollection(1).DataLabels(5).Format.TextFrame2.TextRange. _
InsertChartField msoChartFieldFormula, "='Risiko-Log'!$B$6", 1
End Sub
Obviously, this is not very effective, so I'd like to recreate the approach in a for-Loop, picking each referenced cell using the i counter (Points and DataLabels would use i and the index or cell specification would use i+1).
I've been trying a few different approaches for a while but can't seem to get the syntax right. Does anyone have the idea that'll help me get through this and regain my motivation? Any help is appreciated! Here's where I'm at right now:
Sub Rename_scat()
'
' Rename_scat Macro
'
'
Dim i As Integer
Dim LastRow As Integer
LastRow = Range("'Risiko-Log'!A" & Rows.Count).End(xlUp).Row
For i = 1 To LastRow
ActiveSheet.ChartObjects("Risikomatrix").Activate
ActiveChart.FullSeriesCollection(1).DataLabels.Select
ActiveChart.FullSeriesCollection(1).Points(i).DataLabel.Select
ActiveChart.SeriesCollection(1).DataLabels(i).Format.TextFrame2.TextRange. _
Characters.Text = ""
ActiveChart.SeriesCollection(1).DataLabels(i).Format.TextFrame2.TextRange. _
InsertChartField msoChartFieldFormula, "'Risiko-Log'!.Cells(i + 1, 2)", 1
Next i
End Sub