VBA Chart Formatting to Loop Through all Charts in Workbook

PippaBett

New Member
Joined
Sep 4, 2019
Messages
4
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
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Where does the error occur? I don't even see where you're counting labels.
 
Upvote 0
The error occurs at the line ActiveChart.FullSeriesCollection(1).DataLabels.Select which is where I selected the data labels in the recorded macro
 
Upvote 0
A big problem with recorded macros is that they repeat all the little mouse actions, like selecting everything. And if for some reason you're not showing data labels, perhaps because the chart's source data range is blank and no points are plotted, trying to select them will cause an error.

Before I fix that, I'll point out that there are two nearly identical blocks of code. The first block fills the label rectangles with a dark color.

Code:
WithSelection.Format.Fill
  .Visible =msoTrue
  .ForeColor.ObjectThemeColor = msoThemeColorAccent1
  .ForeColor.TintAndShade = 0
  .ForeColor.Brightness = 0
  .Solid
End With

The second block fills the label rectangles with a light color (white, it looks like).

Code:
WithSelection.Format.Fill
  .Visible =msoTrue
  .ForeColor.ObjectThemeColor= msoThemeColorBackground1
  .ForeColor.TintAndShade = 0
  .ForeColor.Brightness = 0
  .Transparency= 0
  .Solid
End With

So the first block should be deleted.

Another problem with the macro recorder is that it includes code that simply applied default properties. So that second block can be cleaned up:

Code:
WithSelection.Format.Fill
  .Visible =msoTrue
  .ForeColor.ObjectThemeColor= msoThemeColorBackground1
End With

So now let's try a little cleanup.

Code:
  For Each sht In ActiveWorkbook.Worksheets
    For Each cht Insht.ChartObjects
      cht.Activate

      With ActiveChart
        .SetElement msoElementDataLableBestFit

        With .FullSeriesCollection(1).DataLabels
          With .Format.Fill
            .Visible =msoTrue
            .ForeColor.ObjectThemeColor = msoThemeColorBackground1
          End With
          With .Format.TextFrame2
            .MarginLeft = 0
            .MarginRight = 0
            .MarginTop = 0
            .MarginBottom = 0
          End With
        End With
      End With
    Next cht
  Next sht

I don't like using .SetElement, either, but that's a tale for another time.
 
Upvote 0
Hi thanks for your response, unfortunately I am still getting the same error message as before it is now highlighting this portion of text With .FullSeriesCollection(1).DataLabels
 
Upvote 0
I just tested this code on a workbook with multiple worksheets and charts. No errors, each chart ended up with data labels.

Code:
Sub UpdateCharts()
  Dim ws As Worksheet
  For Each ws In ActiveWorkbook.Worksheets
    Dim chtob As ChartObject
    For Each chtob In ws.ChartObjects
      With chtob.Chart
        .SetElement msoElementDataLabelBestFit

        With .FullSeriesCollection(1).DataLabels
          With .Format.Fill
            .Visible = msoTrue
            .ForeColor.ObjectThemeColor = msoThemeColorBackground1
          End With
          With .Format.TextFrame2
            .MarginLeft = 0
            .MarginRight = 0
            .MarginTop = 0
            .MarginBottom = 0
          End With
        End With
      End With
    Next chtob
  Next ws
End Sub

Does the code work on any of your charts, or does it fail on the first one?

Anything noteworthy about the chart that the code fails on? Does it have data labels? Is it not a pie (so best fit is meaningless)? Does it even have any data?
 
Upvote 0
Hi - I have now got it to run and data labels are being added but the formatting is not being applied to them - it is no longer returning an error though
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,853
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top