VBA charts and Series

MRxlicius

New Member
Joined
Apr 5, 2018
Messages
21
I have the following code, to loop through all the charts in a worksheet and all the series in each chart and change for example the number format and the separator. However i am getting an error: on the line "For Each mychart In mysheet.ChartObjects". Could you please help me.

VBA Code:
Sub LoopThroughCharts()

Dim mysheet As Worksheet
Dim CurrentSheet As Worksheet
Dim mychart As ChartObject
Dim myseries As Series


Application.ScreenUpdating = False
Application.EnableEvents = False

Set CurrentSheet = ActiveSheet

  For Each mychart In mysheet.ChartObjects
    mychart.Activate

  For Each myseries  In myChartObject.Chart.SeriesCollection
    Selection.NumberFormat = "#'##0"
    Selection.Separator = "; "
    Next
  
  
  Next

CurrentSheet.Activate
Application.EnableEvents = True

End Sub
 
Last edited:

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
You have dim'd some objects but then not used them, instead using the selection object which at the time in the code is the wrong object
I have assumed the Selection was in reference to the data labels.

As the code is now using the objects there is no need to select or activate anything

VBA Code:
Sub LoopThroughCharts()

Dim mysheet As Worksheet
Dim CurrentSheet As Worksheet
Dim mychart As ChartObject
Dim myseries As Series


Application.ScreenUpdating = False
Application.EnableEvents = False

Set mysheet = ActiveSheet
 
  For Each mychart In mysheet.ChartObjects

  For Each myseries In mychart.Chart.SeriesCollection
    myseries.DataLabels.NumberFormat = "#'##0"
    myseries.DataLabels.Separator = "; "
    Next
 
  Next

Application.EnableEvents = True
Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,756
Messages
6,126,692
Members
449,330
Latest member
ThatGuyCap

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