Finding Chart Objects Based on their series

tias

New Member
Joined
Oct 30, 2009
Messages
5
I need to find all chart objects that use a particular range as a value for a sequence so that I can update them to use a different range instead.

The following is the code I already have
Rich (BB code):
Option Explicit
Sub testUpdateCharts()
    updateCharts Range("A1:A10"), Range("B1:B10")
End Sub

Sub updateCharts(oldvals As Range, newvals As Range)
    Dim chrt As Chart
    'Iterate ALL the charts
    For Each chrt In ThisWorkbook.Charts
        updateChart chrt, oldvals, newvals
    Next chrt
    Dim sheet As Worksheet
    For Each sheet In ThisWorkbook.Worksheets
        Dim t As Variant
        For Each t In sheet.ChartObjects
            Set chrt = t.Chart
            updateChart chrt, oldvals, newvals
        Next t
    Next sheet
End Sub
Sub updateChart(chrt As Chart, oldvals As Range, newvals As Range)
    Debug.Print "UpdateChart: " & chrt.Name
    Dim srs As Series
    'Iterate all the series in the chart
    For Each srs In chrt.SeriesCollection
        Debug.Print "UpdateChart: " & chrt.Name & "::" & srs.Name
        'If srs.Values = oldvals Then 'error 13 Type miss match
        If srs.Values.Cells(1, 1) = oldvals.Cells(1, 1) Then 'errors 424 object required
            Set srs.Values = newvals
        End If
    Next srs
    Exit Sub
End Sub

The comparison fails with an error; caused either by a type miss-match[13] (direct comparison) or object expected[424] (when comparing the top-left cell only)

Any enlightenment as to why the code doesn't seem to work would be greatly appreciated
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Check Excel VBA help for the series.Values. You will find it returns an array with the actual values not a range object.
 
Upvote 0
Many thanks for pointing out my error;
I tested to see what the type of the array is to find that it is an array of values as you had indicated. (although this seems somewhat at odds with the scant documentation microsoft provides on the issue: http://msdn.microsoft.com/en-us/library/bb238636.aspx)

any suggestions or pointers how I might actually find a range object indicating what cells this series is using as data?

Many thanks again, 'Tias
 
Upvote 0
Yeah, I just came to the same conclusion after far too much googling and now have a half working functions to do the job.
I derived my code from
http://www.j-walk.com/ss/excel/tips/tip83.htm
but this particular code will break if any of the parts of the formula contain a comma (eg when the sheet name contains a comma)
 
Upvote 0

Forum statistics

Threads
1,215,042
Messages
6,122,810
Members
449,095
Latest member
m_smith_solihull

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