I have a large dashboard that once complete will have c300 pie charts and need them all to have specific formatting. I have recorded a macro of me applying the formatting to one chart and looked up a macro to loop through all charts in the workbook and tried to combine the two to create the macro I need but I am getting the error message "Run-time error '-2147467259 (80004005)': Unable to get the Count property of the DataLables class". I have spent hours trying to look for a solution to get this to work without having to resort to manually formatting 300 charts!
The code is
Sub LoopThroughCharts()
Dim sht As Worksheet
Dim CurrentSheet As Worksheet
Dim cht As ChartObject
Application.ScreenUpdating = False
Application.EnableEvents = False
Set CurrentSheet = ActiveSheet
For Each sht In ActiveWorkbook.Worksheets
For Each cht Insht.ChartObjects
cht.Activate
cht.Chart.SetElement msoElementDataLableBestFit
ActiveChart.FullSeriesCollection(1).DataLabels.Select
WithSelection.Format.Fill
.Visible =msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Solid
End With
WithSelection.Format.Fill
.Visible =msoTrue
.ForeColor.ObjectThemeColor= msoThemeColorBackground1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency= 0
.Solid
End With
Selection.Format.TextFrame2.MarginLeft = 0
Selection.Format.TextFrame2.MarginRight = 0
Selection.Format.TextFrame2.MarginTop = 0
Selection.Format.TextFrame2.MarginBottom = 0
Next cht
Next sht
CurrentSheet.Activate
Application.EnableEvents = True
End Sub
Does anyone know how I can fix this? Many thanks in advance
The code is
Sub LoopThroughCharts()
Dim sht As Worksheet
Dim CurrentSheet As Worksheet
Dim cht As ChartObject
Application.ScreenUpdating = False
Application.EnableEvents = False
Set CurrentSheet = ActiveSheet
For Each sht In ActiveWorkbook.Worksheets
For Each cht Insht.ChartObjects
cht.Activate
cht.Chart.SetElement msoElementDataLableBestFit
ActiveChart.FullSeriesCollection(1).DataLabels.Select
WithSelection.Format.Fill
.Visible =msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Solid
End With
WithSelection.Format.Fill
.Visible =msoTrue
.ForeColor.ObjectThemeColor= msoThemeColorBackground1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency= 0
.Solid
End With
Selection.Format.TextFrame2.MarginLeft = 0
Selection.Format.TextFrame2.MarginRight = 0
Selection.Format.TextFrame2.MarginTop = 0
Selection.Format.TextFrame2.MarginBottom = 0
Next cht
Next sht
CurrentSheet.Activate
Application.EnableEvents = True
End Sub
Does anyone know how I can fix this? Many thanks in advance