Loop through all charts in workbook

GustavBA

New Member
Joined
Mar 14, 2008
Messages
22
Hi all

I have read some posts here and there about problems with looping through every chart in a workbook. Now I have similar problems and I dont seem to get the suggested alternatives to work better than what I all ready have.

The code I all ready have is code which some other insightfull vba guy on the net have written.

Now the problem I am trying to solve is to breake the links between a chart and its scource data. In my workbook I have several sheets with several charts on them. I want to distribute my workbook to others without the scource data, and that's where trouble begins.

An alternative solution could be to make pictures out of the charts before deleting scource data, but the loop problems would still remais.

Here is the code so far. It runs without errors but fail to loop through all charts:

Code:
Sub break_chart_links()
Dim iCtr As Integer, iChars As Integer, iPlotOrder As Integer
Dim nPts As Long, iPts As Long 'Holds the total no of points in the chart
Dim xArray, yArray, sChtName As String, sSrsName As String
Dim xVals, yVals
Dim ChtSeries As Series ' var used to loop thru the series collection
Dim ws As Worksheet
Dim sChartType As String, iCtr1 As Integer
Dim SH As Shape
 
Application.DisplayAlerts = False
Application.ScreenUpdating = False
 
For Each ws In ActiveWorkbook.Worksheets 
ws.Activate
For Each SH In ws.Shapes
On Error Resume Next
SH.Select
If SH.Type = msoChart Then
sChtName = ActiveChart.Name
For Each ChtSeries In ActiveChart.SeriesCollection
nPts = ChtSeries.Points.Count
xArray = ""
yArray = ""
xVals = ChtSeries.XValues
yVals = ChtSeries.Values
sSrsName = ChtSeries.Name
iPlotOrder = ChtSeries.PlotOrder
For iPts = 1 To nPts
If IsNumeric(xVals(iPts)) Then
' SHORTEN NOS IN X ARRAY (REMOVING EXCESS DIGITS)
iChars = WorksheetFunction.Max(InStr(CStr(xVals(iPts)), "."), 5)
xArray = xArray & Left(CStr(xVals(iPts)), iChars) & ","
Else
'PUTTING QUOTES AROUND STRING VALUES
xArray = xArray & """" & xVals(iPts) & ""","
End If
'SAME AS X (ABOVE)
iChars = WorksheetFunction.Max(InStr(CStr(yVals(iPts)), "."), 5)
 
''' handle missing data - replace blanks and #N/A with #N/A
If IsEmpty(yVals(iPts)) Or WorksheetFunction.IsNA(yVals(iPts)) Then
yArray = yArray & "#N/A,"
Else
' NEED TO ROUND NUMBERS ELSE THROWS ERROR
yArray = yArray & Round(Left(CStr(yVals(iPts)), iChars), 0) & ","
End If
Next
'REMOVE FINAL COMMA
xArray = Left(xArray, Len(xArray) - 1)
yArray = Left(yArray, Len(yArray) - 1)
 
With ChtSeries
.Values = yArray
.XValues = xArray
.Name = sSrsName
.PlotOrder = iPlotOrder
End With 
 
Next
End If
Next SH 
Next 
End Sub

In stead of using For each I have tried for i = 1 to ws.ChartObjects.Count but with no improvement.
 
Last edited:

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Now this showed out to be pretty simple after all.

This showed to work:

Rich (BB code):
Sub break_chart_links()
Dim iCtr As Integer, iChars As Integer, iPlotOrder As Integer
Dim nPts As Long, iPts As Long 'Holds the total no of points in the chart
Dim xArray, yArray, sChtName As String, sSrsName As String
Dim xVals, yVals
Dim ChtSeries As Series ' var used to loop thru the series collection
Dim ws As Worksheet
Dim sChartType As String, iCtr1 As Integer
Dim SH As Shape
Dim shp As Shape, chtObj As ChartObject, cht As Chart
 
 
Application.DisplayAlerts = False
Application.ScreenUpdating = False
 
For Each ws In ActiveWorkbook.Worksheets 
ws.Activate
For i = 1 To ActiveSheet.ChartObjects.Count
On Error Resume Next
ActiveSheet.ChartObjects(i).Select
If ActiveChart.Type = msoChart Then 
 
Set chtObj = shp.DrawingObject
Set cht = chtObj.Chart
sChtName = ActiveChart.Name
For Each ChtSeries In ActiveChart.SeriesCollection
nPts = ChtSeries.Points.Count
xArray = ""
yArray = ""
xVals = ChtSeries.XValues
yVals = ChtSeries.Values
sSrsName = ChtSeries.Name
iPlotOrder = ChtSeries.PlotOrder
For iPts = 1 To nPts
If IsNumeric(xVals(iPts)) Then
' SHORTEN NOS IN X ARRAY (REMOVING EXCESS DIGITS)
iChars = WorksheetFunction.Max(InStr(CStr(xVals(iPts)), "."), 5) 
xArray = xArray & Left(CStr(xVals(iPts)), iChars) & ","
Else
'PUTTING QUOTES AROUND STRING VALUES
xArray = xArray & """" & xVals(iPts) & ""","
End If
'SAME AS X (ABOVE)
iChars = WorksheetFunction.Max(InStr(CStr(yVals(iPts)), "."), 5)
 
''' handle missing data - replace blanks and #N/A with #N/A
If IsEmpty(yVals(iPts)) Or WorksheetFunction.IsNA(yVals(iPts)) Then
yArray = yArray & "#N/A,"
Else
' NEED TO ROUND NUMBERS ELSE THROWS ERROR
yArray = yArray & Round(Left(CStr(yVals(iPts)), iChars), 0) & ","
End If
Next
'REMOVE FINAL COMMA
xArray = Left(xArray, Len(xArray) - 1)
yArray = Left(yArray, Len(yArray) - 1)
 
With ChtSeries
.Values = yArray
.XValues = xArray
.Name = sSrsName
.PlotOrder = iPlotOrder
End With 
 
Next
End If
Next i 
 
Next
 
ActiveWorkbook.Save
 
' Call Get_wb_ready_for_distribution
 
End Sub
 
Upvote 0
#¤%#¤% Disregard my last post. That code didn't "remember" looping through all charts afterall. :mad:
 
Upvote 0

Forum statistics

Threads
1,214,628
Messages
6,120,618
Members
448,973
Latest member
ChristineC

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