Question : Excel Charts - Programmatically updating series ranges

Hi All,

I am trying to get some VBA code to programmatically update series ranges in charts (there are lots of them to do).

However, it is getting stuck on some (having no effect).

My code is as follows:

Option Explicit

Sub ChartDataFixup()

Dim MySht As Worksheet
Dim MyCht As ChartObject
Dim MySeries As Series

    On Error Resume Next

    For Each MySht In ThisWorkbook.Worksheets
   
        If Left(MySht.Name, 3) = "Cht" Then
   
            Debug.Print "++++++"
            Debug.Print MySht.Name
   
            For Each MyCht In MySht.ChartObjects
       
                For Each MySeries In MyCht.Chart.SeriesCollection
       
                    Debug.Print "BEFORE: " & MySeries.Name & " " & MySeries.Formula
       
                    MySeries.Formula = Replace(MySeries.Formula, "$42,", "$999,")
                   
                    Debug.Print "AFTER : " & MySeries.Name & " " & MySeries.Formula
                   
                Next
       
            Next
       
        End If
   
    Next

    Debug.Print "++++++"

End Sub


The output in the immediate window is like this (I've only pasted the first three, but there are dozens):

++++++
Cht - Revenue - Month
BEFORE: A =SERIES(Data!$E$5,'SPS Projection.xls'!Chart_Period,Data!$E$7:$E$999,1)
AFTER : A =SERIES(Data!$E$5,'SPS Projection.xls'!Chart_Period,Data!$E$7:$E$999,1)
BEFORE: B =SERIES(Data!$F$5,'SPS Projection.xls'!Chart_Period,Data!$F$7:$F$999,2)
AFTER : B =SERIES(Data!$F$5,'SPS Projection.xls'!Chart_Period,Data!$F$7:$F$999,2)
BEFORE: C =SERIES(Data!$G$5,'SPS Projection.xls'!Chart_Period,Data!$G$7:$G$999,3)
AFTER : C =SERIES(Data!$G$5,'SPS Projection.xls'!Chart_Period,Data!$G$7:$G$999,3)
BEFORE: D =SERIES(Data!$H$5,'SPS Projection.xls'!Chart_Period,Data!$H$7:$H$999,4)
AFTER : D =SERIES(Data!$H$5,'SPS Projection.xls'!Chart_Period,Data!$H$7:$H$999,4)
BEFORE: E =SERIES(Data!$I$5,'SPS Projection.xls'!Chart_Period,Data!$I$7:$I$999,5)
AFTER : E =SERIES(Data!$I$5,'SPS Projection.xls'!Chart_Period,Data!$I$7:$I$999,5)
BEFORE: F =SERIES(Data!$J$5,'SPS Projection.xls'!Chart_Period,Data!$J$7:$J$999,6)
AFTER : F =SERIES(Data!$J$5,'SPS Projection.xls'!Chart_Period,Data!$J$7:$J$999,6)
BEFORE: G =SERIES(Data!$K$5,'SPS Projection.xls'!Chart_Period,Data!$K$7:$K$999,7)
AFTER : G =SERIES(Data!$K$5,'SPS Projection.xls'!Chart_Period,Data!$K$7:$K$999,7)
BEFORE: Total (RHS) =SERIES(Data!$N$5,,Data!$N$7:$N$999,8)
AFTER : Total (RHS) =SERIES(Data!$N$5,,Data!$N$7:$N$999,8)
++++++
Cht - Gross Profit- Month
BEFORE: A =SERIES(Data!$AC$5,'SPS Projection.xls'!Chart_Period,Data!$AC$7:$AC$999,1)
AFTER : A =SERIES(Data!$AC$5,'SPS Projection.xls'!Chart_Period,Data!$AC$7:$AC$999,1)
BEFORE: B =SERIES(Data!$AD$5,'SPS Projection.xls'!Chart_Period,Data!$AD$7:$AD$999,2)
AFTER : B =SERIES(Data!$AD$5,'SPS Projection.xls'!Chart_Period,Data!$AD$7:$AD$999,2)
BEFORE: C =SERIES(Data!$AE$5,'SPS Projection.xls'!Chart_Period,Data!$AE$7:$AE$999,3)
AFTER : C =SERIES(Data!$AE$5,'SPS Projection.xls'!Chart_Period,Data!$AE$7:$AE$999,3)
BEFORE: D =SERIES(Data!$AF$5,'SPS Projection.xls'!Chart_Period,Data!$AF$7:$AF$999,4)
AFTER : D =SERIES(Data!$AF$5,'SPS Projection.xls'!Chart_Period,Data!$AF$7:$AF$999,4)
BEFORE: E =SERIES(Data!$AG$5,'SPS Projection.xls'!Chart_Period,Data!$AG$7:$AG$999,5)
AFTER : E =SERIES(Data!$AG$5,'SPS Projection.xls'!Chart_Period,Data!$AG$7:$AG$999,5)
BEFORE: Total (RHS) =SERIES(Data!$AL$5,,Data!$AL$7:$AL$999,6)
AFTER : Total (RHS) =SERIES(Data!$AL$5,,Data!$AL$7:$AL$999,6)
BEFORE: F =SERIES(Data!$AH$5,'SPS Projection.xls'!Chart_Period,Data!$AH$7:$AH$999,7)
AFTER : F =SERIES(Data!$AH$5,'SPS Projection.xls'!Chart_Period,Data!$AH$7:$AH$999,7)
BEFORE: G =SERIES(Data!$AI$5,'SPS Projection.xls'!Chart_Period,Data!$AI$7:$AI$999,8)
AFTER : G =SERIES(Data!$AI$5,'SPS Projection.xls'!Chart_Period,Data!$AI$7:$AI$999,8)
++++++
Cht - GP% - Month
BEFORE: A =SERIES(Data!$BA$5,'SPS Projection.xls'!Chart_Period,Data!$BA$7:$BA$42,1)
AFTER : A =SERIES(Data!$BA$5,'SPS Projection.xls'!Chart_Period,Data!$BA$7:$BA$42,1)
BEFORE: B =SERIES(Data!$BB$5,'SPS Projection.xls'!Chart_Period,Data!$BB$7:$BB$42,2)
AFTER : B =SERIES(Data!$BB$5,'SPS Projection.xls'!Chart_Period,Data!$BB$7:$BB$42,2)
BEFORE: D =SERIES(Data!$BD$5,'SPS Projection.xls'!Chart_Period,Data!$BD$7:$BD$42,3)
AFTER : D =SERIES(Data!$BD$5,'SPS Projection.xls'!Chart_Period,Data!$BD$7:$BD$42,3)
BEFORE: E =SERIES(Data!$BE$5,'SPS Projection.xls'!Chart_Period,Data!$BE$7:$BE$42,4)
AFTER : E =SERIES(Data!$BE$5,'SPS Projection.xls'!Chart_Period,Data!$BE$7:$BE$42,4)
BEFORE: F =SERIES(Data!$BF$5,'SPS Projection.xls'!Chart_Period,Data!$BF$7:$BF$42,5)
AFTER : F =SERIES(Data!$BF$5,'SPS Projection.xls'!Chart_Period,Data!$BF$7:$BF$42,5)
BEFORE: G =SERIES(Data!$BG$5,'SPS Projection.xls'!Chart_Period,Data!$BG$7:$BG$42,6)
AFTER : G =SERIES(Data!$BG$5,'SPS Projection.xls'!Chart_Period,Data!$BG$7:$BG$42,6)
++++++


As you can see, the code has (previous run) fixed the first two charts, but has failed on the third chart.

What am I missing here?

Thanks,

Alan.

Answer : Excel Charts - Programmatically updating series ranges

This works for me, using R1C1 style notation. Give it a try please :-)


                    Debug.Print "BEFORE: " & MySeries.Name & " " & MySeries.FormulaR1C1
                    MySeries.FormulaR1C1 = Replace(MySeries.FormulaR1C1, "R42C", "R99C")
                    Debug.Print "AFTER : " & MySeries.Name & " " & MySeries.FormulaR1C1
Random Solutions  
 
programming4us programming4us