'VBA doesn´t add datalabel
I have an existent graph to which I added a macros. The macros should update the axes, add a datalabel to the last point and delete the datalabel of the previous point every time I update the database. However, the code doesn't recognize the last datalabel and doesn't add the next the first time I run the macros and shows error in the following line: ActiveChart.FullSeriesCollection(1).Points(ult2).DataLabel.Select
I tried deleting that part and run the macros and add that for the second test. The datalabel is added but the third time it is not added again.
I have run a test on another workbook and the macros runs perfectly on that.
I hope someone can help me.
The full code is as follows:
Sheets(1).Select
ult = Cells(Rows.Count, 1).End(xlUp).Row - 1
ult2 = ult - 1
Sheets(2).Select
ActiveSheet.ChartObjects("Gráfico 4").Activate
ActiveChart.Axes(xlCategory).Select
ActiveChart.Axes(xlCategory).MinimumScale = CDate(Sheets(1).Cells((ult - 23), 1).Value)
ActiveChart.Axes(xlCategory).MaximumScale = CDate(Sheets(1).Cells(ult + 1, 1).Value)
ActiveChart.FullSeriesCollection(1).Select
ActiveChart.FullSeriesCollection(1).Points(ult).Select
ActiveChart.FullSeriesCollection(1).Points(ult).ApplyDataLabels
ActiveChart.FullSeriesCollection(1).Points(ult2).DataLabel.Select
Selection.Delete
ActiveSheet.ChartObjects("Gráfico 4").Activate
ActiveChart.FullSeriesCollection(1).Select
ActiveChart.FullSeriesCollection(1).Points(ult).Select
ActiveChart.FullSeriesCollection(1).Points(ult).ApplyDataLabels
ActiveChart.FullSeriesCollection(1).DataLabels.Select
ActiveSheet.ChartObjects("Gráfico 4").Activate
ActiveChart.FullSeriesCollection(1).DataLabels.Select
ActiveChart.FullSeriesCollection(1).Points(ult).DataLabel.Select
With Selection.Format.TextFrame2.TextRange.Font
.NameComplexScript = "Humanst521 BT"
.NameFarEast = "Humanst521 BT"
.Name = "Humanst521 BT"
End With
Selection.Format.TextFrame2.TextRange.Font.Bold = msoTrue
With Selection.Format.TextFrame2.TextRange.Font.Fill
.Visible = msoTrue
.ForeColor.RGB = RGB(0, 32, 96)
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
.Solid
End With
Selection.Format.TextFrame2.TextRange.Font.Size = 10
ActiveChart.SeriesCollection(1).DataLabels(ult).Format.TextFrame2.TextRange. _
Characters.Text = ""
ActiveChart.SeriesCollection(1).DataLabels(ult).Format.TextFrame2.TextRange. _
InsertChartField msoChartFieldValue, "", 1
With ActiveChart.SeriesCollection(1).DataLabels(ult).Format.TextFrame2.TextRange _
.Font
.Bold = msoTrue
.Fill.Visible = msoTrue
.Fill.ForeColor.RGB = RGB(0, 32, 96)
.Fill.ForeColor.TintAndShade = 0
.Fill.ForeColor.Brightness = 0
.Fill.Transparency = 0
.Fill.Solid
.Size = 10
.Name = "Humanst521 BT"
End With
With ActiveChart.SeriesCollection(1).DataLabels(ult).Format.TextFrame2.TextRange _
.ParagraphFormat
.TextDirection = msoTextDirectionLeftToRight
.Alignment = msoAlignCenter
End With
End Sub
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|