Microsoft
Software
Hardware
Network
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.SeriesCollecti
on
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_Peri
od,Data!$E
$7:$E$999,
1)
AFTER : A =SERIES(Data!$E$5,'SPS Projection.xls'!Chart_Peri
od,Data!$E
$7:$E$999,
1)
BEFORE: B =SERIES(Data!$F$5,'SPS Projection.xls'!Chart_Peri
od,Data!$F
$7:$F$999,
2)
AFTER : B =SERIES(Data!$F$5,'SPS Projection.xls'!Chart_Peri
od,Data!$F
$7:$F$999,
2)
BEFORE: C =SERIES(Data!$G$5,'SPS Projection.xls'!Chart_Peri
od,Data!$G
$7:$G$999,
3)
AFTER : C =SERIES(Data!$G$5,'SPS Projection.xls'!Chart_Peri
od,Data!$G
$7:$G$999,
3)
BEFORE: D =SERIES(Data!$H$5,'SPS Projection.xls'!Chart_Peri
od,Data!$H
$7:$H$999,
4)
AFTER : D =SERIES(Data!$H$5,'SPS Projection.xls'!Chart_Peri
od,Data!$H
$7:$H$999,
4)
BEFORE: E =SERIES(Data!$I$5,'SPS Projection.xls'!Chart_Peri
od,Data!$I
$7:$I$999,
5)
AFTER : E =SERIES(Data!$I$5,'SPS Projection.xls'!Chart_Peri
od,Data!$I
$7:$I$999,
5)
BEFORE: F =SERIES(Data!$J$5,'SPS Projection.xls'!Chart_Peri
od,Data!$J
$7:$J$999,
6)
AFTER : F =SERIES(Data!$J$5,'SPS Projection.xls'!Chart_Peri
od,Data!$J
$7:$J$999,
6)
BEFORE: G =SERIES(Data!$K$5,'SPS Projection.xls'!Chart_Peri
od,Data!$K
$7:$K$999,
7)
AFTER : G =SERIES(Data!$K$5,'SPS Projection.xls'!Chart_Peri
od,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_Peri
od,Data!$A
C$7:$AC$99
9,1)
AFTER : A =SERIES(Data!$AC$5,'SPS Projection.xls'!Chart_Peri
od,Data!$A
C$7:$AC$99
9,1)
BEFORE: B =SERIES(Data!$AD$5,'SPS Projection.xls'!Chart_Peri
od,Data!$A
D$7:$AD$99
9,2)
AFTER : B =SERIES(Data!$AD$5,'SPS Projection.xls'!Chart_Peri
od,Data!$A
D$7:$AD$99
9,2)
BEFORE: C =SERIES(Data!$AE$5,'SPS Projection.xls'!Chart_Peri
od,Data!$A
E$7:$AE$99
9,3)
AFTER : C =SERIES(Data!$AE$5,'SPS Projection.xls'!Chart_Peri
od,Data!$A
E$7:$AE$99
9,3)
BEFORE: D =SERIES(Data!$AF$5,'SPS Projection.xls'!Chart_Peri
od,Data!$A
F$7:$AF$99
9,4)
AFTER : D =SERIES(Data!$AF$5,'SPS Projection.xls'!Chart_Peri
od,Data!$A
F$7:$AF$99
9,4)
BEFORE: E =SERIES(Data!$AG$5,'SPS Projection.xls'!Chart_Peri
od,Data!$A
G$7:$AG$99
9,5)
AFTER : E =SERIES(Data!$AG$5,'SPS Projection.xls'!Chart_Peri
od,Data!$A
G$7:$AG$99
9,5)
BEFORE: Total (RHS) =SERIES(Data!$AL$5,,Data!$
AL$7:$AL$9
99,6)
AFTER : Total (RHS) =SERIES(Data!$AL$5,,Data!$
AL$7:$AL$9
99,6)
BEFORE: F =SERIES(Data!$AH$5,'SPS Projection.xls'!Chart_Peri
od,Data!$A
H$7:$AH$99
9,7)
AFTER : F =SERIES(Data!$AH$5,'SPS Projection.xls'!Chart_Peri
od,Data!$A
H$7:$AH$99
9,7)
BEFORE: G =SERIES(Data!$AI$5,'SPS Projection.xls'!Chart_Peri
od,Data!$A
I$7:$AI$99
9,8)
AFTER : G =SERIES(Data!$AI$5,'SPS Projection.xls'!Chart_Peri
od,Data!$A
I$7:$AI$99
9,8)
++++++
Cht - GP% - Month
BEFORE: A =SERIES(Data!$BA$5,'SPS Projection.xls'!Chart_Peri
od,Data!$B
A$7:$BA$42
,1)
AFTER : A =SERIES(Data!$BA$5,'SPS Projection.xls'!Chart_Peri
od,Data!$B
A$7:$BA$42
,1)
BEFORE: B =SERIES(Data!$BB$5,'SPS Projection.xls'!Chart_Peri
od,Data!$B
B$7:$BB$42
,2)
AFTER : B =SERIES(Data!$BB$5,'SPS Projection.xls'!Chart_Peri
od,Data!$B
B$7:$BB$42
,2)
BEFORE: D =SERIES(Data!$BD$5,'SPS Projection.xls'!Chart_Peri
od,Data!$B
D$7:$BD$42
,3)
AFTER : D =SERIES(Data!$BD$5,'SPS Projection.xls'!Chart_Peri
od,Data!$B
D$7:$BD$42
,3)
BEFORE: E =SERIES(Data!$BE$5,'SPS Projection.xls'!Chart_Peri
od,Data!$B
E$7:$BE$42
,4)
AFTER : E =SERIES(Data!$BE$5,'SPS Projection.xls'!Chart_Peri
od,Data!$B
E$7:$BE$42
,4)
BEFORE: F =SERIES(Data!$BF$5,'SPS Projection.xls'!Chart_Peri
od,Data!$B
F$7:$BF$42
,5)
AFTER : F =SERIES(Data!$BF$5,'SPS Projection.xls'!Chart_Peri
od,Data!$B
F$7:$BF$42
,5)
BEFORE: G =SERIES(Data!$BG$5,'SPS Projection.xls'!Chart_Peri
od,Data!$B
G$7:$BG$42
,6)
AFTER : G =SERIES(Data!$BG$5,'SPS Projection.xls'!Chart_Peri
od,Data!$B
G$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.FormulaR1
C1, "R42C", "R99C")
Debug.Print "AFTER : " & MySeries.Name & " " & MySeries.FormulaR1C1
Random Solutions
Would you recommend Draytek Vigor 2820 for site-to-site VPN
xml to array
Group policy updates causes screen to flicker
Windows 2003 Server Restart Errors
Iseries OSV5R4 PM eserver iSeries telephone number
Create a list of users in Active Directory whose account has been locked out for 90 days
Training Path
Filemaker Pro Summary Report - Obtraining Fractions from Summary Field
VPN problems: SonicWall TZ 180 to TZ 210
Function in excel