Change Series Formula – Improved Routines
by Jon Peltier
Monday, March 28th, 2011
Peltier Technical Services, Inc., Copyright © 2012.
Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
Ever make a copy of a chart, and all you want is do is change the worksheet containing the source data? Or change the X values of all series from column A to column B? It sure is a pain to edit each series’ source data range of series formula one by one.
The series formula is a simple text string, but there’s no Search and Replace feature in Excel that can access these formulas. But you can use some very simple VBA code to make wholesale changes to chart series formulas. Or you can skip all of the noise, scroll to the end of this article, and download the new Change Series Formula Utility.
About the Chart Series Formula
A more detailed description of a chart and its series formula is presented in The Chart Series Formula and How to Edit Series Formulas elsewhere on this site, but a brief description here is in order.
Every chart series has a formula which describes the data in the series. The simple chart in this example is shown below its data range; the series is selected to highlight the data used to plot the series and to show the SERIES formula in the formula bar.

=SERIES(Sheet1!$B$1,Sheet1!$A$2:$A$11,Sheet1!$B$2:$B$11,1)
This formula can be broken up into four elements as follows:
=SERIES([Series Name],[X Values],[Y Values],[Plot Order])
In our example:
=SERIES(Sheet1!$B$1,Sheet1!$A$2:$A$11,Sheet1!$B$2:$B$11,1)
Sheet1!$B$1 contains the Series Name
=SERIES(Sheet1!$B$1,Sheet1!$A$2:$A$11,Sheet1!$B$2:$B$11,1)
Sheet1!$A$2:$A$11 contains the X Values or Category Labels
=SERIES(Sheet1!$B$1,Sheet1!$A$2:$A$11,Sheet1!$B$2:$B$11,1)
Sheet1!$B$2:$B$11 contains the Y Values
=SERIES(Sheet1!$B$1,Sheet1!$A$2:$A$11,Sheet1!$B$2:$B$11,1)
and the series is plotted first (1) among the chart’s series collection.
The Series Name can be blank, a text string in double quotation marks, a reference to a worksheet range (one or more cells), or a reference to a named range (named formula). The X Values can be blank, a literal array of numeric values or text labels enclosed in curly braces, a reference to a worksheet range (one or more cells), or a reference to a named range (named formula). The Y Values can be a literal array of numeric values enclosed in curly braces, a reference to a worksheet range (one or more cells), or a reference to a named range (named formula). The Plot Order can only be a whole number between 1 and the number of series in the chart.
Editing the Series Formula
The series formula is a text-based Excel formula like any other. You can click in the formula bar, and edit the formula manually, to change aspects of the charted series. Select part or all of the formula, type in the text you want there instead, and press Enter to apply the changes (or sometimes to cause an error!). Alternatively you can select one range address in the formula, then click and drag with the mouse to insert the address of another range in the worksheet.
But if you have to do this for every series in your chart, and every chart on the worksheet, you will be editing in the formula bar all day. So let’s make VBA do some of the tedious work for us.
Editing the Series Formula with VBA
The meat of this technique involves this very simple VBA command:
Series.Formula = WorksheetFunction.Substitute(Series.Formula, OldString, NewString)
Basically, Substitute works by replacing every instance of OldString in the input text string (Series.Formula) by NewString. You could use Replace instead of WorksheetFunction.Substitute, but this would not be compatible with Excel 97.
There are a few tricks I’ve learned to improve the reliability of this technique. First, OldString should be entered using the same case as is found in the formula. For example, “b” will not find any references to column B in the formula. Second, to make sure you don’t trash any worksheet name in the formula, use “$C$” rather than “C” if you are replacing references to column C.
In the simplest version of this utility, you can use a couple input boxes to ask the user for OldString and NewString. A little error trapping makes sure that there is an active chart and that OldString is valid.
Program to Modify the Active Chart
Sub ChangeSeriesFormula()
''' Just do active chart
If ActiveChart Is Nothing Then
'' There is no active chart
MsgBox "Please select a chart and try again.", vbExclamation, _
"No Chart Selected"
Exit Sub
End If
Dim OldString As String, NewString As String, strTemp As String
Dim mySrs As Series
OldString = InputBox("Enter the string to be replaced:", "Enter old string")
If Len(OldString) > 1 Then
NewString= InputBox("Enter the string to replace " & """" _
& OldString & """:", "Enter new string")
'' Loop through all series
For Each mySrs In ActiveChart.SeriesCollection
strTemp = WorksheetFunction.Substitute(mySrs.Formula, OldString, NewString)
mySrs.Formula = strTemp
Next
Else
MsgBox "Nothing to be replaced.", vbInformation, "Nothing Entered"
End If
End Sub
Program to Modify All Charts on the Active Sheet
Adding another loop to iterate through a sheet’s chart objects will change every chart on the sheet.
Sub ChangeSeriesFormulaAllCharts()
''' Do all charts in sheet
Dim oChart As ChartObject
Dim OldString As String, NewString As String
Dim mySrs As Series
OldString = InputBox("Enter the string to be replaced:", "Enter old string")
If Len(OldString) > 1 Then
NewString = InputBox("Enter the string to replace " & """" _
& OldString & """:", "Enter new string")
For Each oChart In ActiveSheet.ChartObjects
For Each mySrs In oChart.Chart.SeriesCollection
mySrs.Formula = WorksheetFunction.Substitute(mySrs.Formula, OldString, NewString)
Next
Next
Else
MsgBox "Nothing to be replaced.", vbInformation, "Nothing Entered"
End If
End Sub
How the program works
Here is our chart again, bet we’ve got an extra column of data. The chart shows column B plotted, but we want to plot column C instead.

Using the second version of the program above, all we need to do is run the code. I usually like to select one chart’s series, to remind me what string I want to change.
The program’s first input box asks for the string we want to replace ($B$ for column B):

The second input box asks for the string we want to replace that string with (“c”):

We see that the program has changed the series formula, while the chart now highlights the new range and shows the new data.

Warning: Blank Line or XY Series
When a line or XY series has no plottable points (that is, the data range has no numerical values to plot), VBA can’t read the formula. This example shows the problem.
We have no trouble changing the chart’s Y values from column B

to column C

even though column C has no data to plot. The chart merely looks blank, and we cannot select the series.
When we try to change from column C

to column D

Excel chokes, because it can’t read the series formula we want it to change.

Excel can’t read the formula for a line or XY series with no data, but fortunately it can read other types, so we can do just fine if we change the series temporarily to a column chart, edit the series, and change the series back. Here’s the code:
Sub ChangeSeriesFormulaAllCharts()
''' Do all charts in sheet
Dim oChart As ChartObject
Dim OldString As String, NewString As String
Dim mySrs As Series
Dim iChartType As XlChartType
Dim sFormula As String
OldString = InputBox("Enter the string to be replaced:", "Enter old string")
If Len(OldString) > 1 Then
NewString = InputBox("Enter the string to replace " & """" _
& OldString & """:", "Enter new string")
For Each oChart In ActiveSheet.ChartObjects
For Each mySrs In oChart.Chart.SeriesCollection
sFormula = ""
On Error Resume Next
sFormula = mySrs.Formula
On Error GoTo 0
' change to column chart if series is inaccessible
If Len(sFormula) = 0 Then
iChartType = mySrs.ChartType
mySrs.ChartType = xlColumnClustered
End If
mySrs.Formula = WorksheetFunction.Substitute(mySrs.Formula, OldString, NewString)
If Len(sFormula) = 0 Then mySrs.ChartType = iChartType
Next
Next
Else
MsgBox "Nothing to be replaced.", vbInformation, "Nothing Entered"
End If
End Sub
The intermediate chart still looks blank, but if we are attentive we might notice the horizontal axis has changed from the typical XY value-style axis to a typical column category-style axis.

After the series is changed, the columns reflect the new values.

Finally, the chart is reverted to its original XY style.

Warning: VBA Error
If the X value argument of a series formula consists of a Name instead of an array or a range address, VBA incorrectly reads the series formula, by enclosing the Name in single quotes. When assigning a formula to a series with a Name as its X value argument, VBA fails whether or not the erroneous single quotes are included. This is apparently a bug in Excel VBA, and it has been reported to Microsoft. The utility below has been upgraded with a workaround to avoid this error.
Essentially the workaround involves checking for a Name in the X values argument of the formula, temporarily removing the argument altogether from the new series formula, then reassigning the Name using the Series.XValues property.
The Change Series Formula Utility
I’ve updated the utility to take into account the funny VBA behavior when Names are used in the formula, and the problem with XY and Line charts that have no data to plot. The updated utility is located in PTS_ChangeSeriesFormula.zip. You can install it following the protocol in Installing an Excel Add-In.
Upon installation, the utility creates a toolbar (In Excel 2007, the toolbar buttons are buried on the Add-Ins tab of the ribbon).

This utility incorporates all of the editing code. In addition, it replaces the separate old and new text input boxes with a single dialog with two text boxes. It also has two buttons, which based on what is selected and active, allows you to change the active chart, change any charts which you’ve selected using Ctrl+Click, or change all charts on the active sheet. It also remembers the inputs in the two text boxes, so you can reuse them for charts on another worksheet. Here is the Change Series Formula dialog:

Try this new utility, and please let me know if it doesn’t work in the comments below.
Related Posts:
- How to Edit Series Formulas
- Make Your Recorded Macro Independent of Which Sheet is Active
- UDF to Calculate an Arbitrary Formula
- Dynamic Chart Source Data
- Make a Copied Chart Link to New Data
- Chart Series Data Highlighting
Posted: Monday, March 28th, 2011 under Utilities.
Comments: 30
Comments
Comment from Mason
Time: Wednesday, March 30, 2011, 5:56 pm
Worked Great! Thanks!
Comment from Cristian
Time: Wednesday, March 30, 2011, 7:03 pm
“Excel chokes, because it can’t read the series formula we want it to change”
As Microsoft says: http://support.microsoft.com/default.aspx?scid=kb;en-us;213379
Is a great bug… I´m trying to find new ways for a clean workaround. I think I have clean one and would like to share it.
Ok, when you try to do things like:
with activechart.seriescollection(1)
.Name = “Imgoingtocrash”
.Values = blabla
.Xvalues = blabla
end with
it will crash if the series cant find any value (in my case I deleted the cells from which Values and Xvalues were taken)
Well I´ve designed this (for the moment) “clean” workaround:
With ActiveChart.SeriesCollection.NewSeries ‘(in this case will be seriescollection(2))
.Name = “Im not going to crash”
.Values = blabla
.Xvalues = blabla
End With
With ActiveChart
.SeriesCollection(1).Name = .SeriesCollection(2).Name
.SeriesCollection(1).XValues = .SeriesCollection(2).XValues
.SeriesCollection(1).Values = .SeriesCollection(2).Values
.SeriesCollection(2).Delete
End With
With that, SeriesCollection(1) is safe and restored
Hope it helps over there for someone.
Cheers and thanks for your nice web.
Comment from Anonymous
Time: Friday, May 20, 2011, 1:15 am
This wroked fantastically – saved hours of my life!
Comment from George Mitchell
Time: Thursday, June 16, 2011, 5:41 pm
John, you have helped me before with some other charting questions, but this is by far the greatest invention since sliced bread! This will shave hours off my updates for my monthly reports! Thank you for sharing this great piece of coding!
Comment from Jacob Piers-Blundell
Time: Tuesday, July 12, 2011, 11:16 pm
Great little utility, saved many hours for me!
Only little issue I had is my sheet had a comma in the name and this caused an endless error loop, once I figured that out, all worked perfectly!
Comment from Jon Peltier
Time: Wednesday, July 13, 2011, 8:05 am
Yes, there are a few things I haven’t yet written around. Commas in the sheet name, single quotes in the sheet name, etc. But these are things I don’t do much myself, so my motivation is limited.
Comment from Richie Mulcahy
Time: Thursday, July 21, 2011, 10:41 am
I am running into a problem with this add-in. I am attempting to change x and y values location from a global scope (NEW ASR DATABASE.xls) to a worksheet scope (CF-DATA). Shown below is an example of one of my series formulas:
=SERIES(‘CF-DATA’!$AL$25,’NEW ASR DATABASE3.xls’!CF_X23,’NEW ASR DATABASE3.xls’!CF_Y23,17)
Shown below is an example of what I would like it to change to:
=SERIES(‘CF-DATA’!$AL$25,’CF-DATA’!CF_X23,’CF-DATA’!CF_Y23,17)
As you can see I am attempting to swap out the ‘NEW ASR DATABASE3.xls’! with ‘CF-DATA’!. I just can’t seem to get it to work, no such change occurs.
Comment from Jon Peltier
Time: Thursday, July 21, 2011, 11:21 am
Richie -
I just tried this on my machine, in both Excel 2003 and Excel 2007, and it changed workbook name (in single quotes) to worksheet name in the series formula with no problem.
Note that the text entered into the “Find old text” box must exactly match capitalization of the text in the series formula.
Comment from Richie Mulcahy
Time: Thursday, July 21, 2011, 11:29 am
Thank you for your quick response. I must be doing something wrong as I cannot get it to change from workbook name to sheet name. Maybe the issue is when I localised my named ranges, which in turn caused invalid named ranges on my chart? When I then run the add-in, perhaps it cannot find the invalid named ranges?
Comment from Jon Peltier
Time: Thursday, July 21, 2011, 1:06 pm
Richie -
Good point. If the current ranges are invalid, and the chart is a line or XY type, VBA cannot access the series formulas. At least it couldn’t in Excel 2003 and earlier. I did a quick test in 2007, and it seems to work regardless of source data validity.
The old trick in 2003 was to change the series to column, make the change, then change back.
Comment from Anonymous
Time: Tuesday, August 2, 2011, 1:57 pm
Both versions worked great. Thank you very much for posting these solutions!
Comment from Anonymous
Time: Thursday, August 11, 2011, 6:47 am
Great utility that saved me loads of time.
Is there anyway of working it to search and replace all graphs within a workbook rather than just the active sheet?
Comment from Jeff Pierce
Time: Friday, August 12, 2011, 6:08 pm
Works great for data series in charts – any way to extend the idea to error bar series and embedded text in charts that is linked to the worksheet as well?
Comment from peter
Time: Friday, September 2, 2011, 11:29 am
great tool – worked nice for me! i’d have the same question as jeff – any way to extend this tool to error bar series?
thanks!
Comment from Vishal
Time: Thursday, September 15, 2011, 7:37 am
this is a great piece of work.
Comment from Kenneth
Time: Wednesday, September 21, 2011, 6:20 pm
This is great for changing columns, do you have an equal way for changing rows using same column?
I have worksheets with 100.000+ rows and like to change the xvalues and values so the user can scroll using buttons.
I have everything ready except an effective way to only change the row part of each seriescollection in chart.
=SERIES(‘sheet1′!$Q$1,’sheet1′!$E$2:$E$10000,’sheett’!$Q$2:$Q$10000,1)”
=SERIES(‘sheet1′!$Q$1,’sheet1′!$E$10000:$E$20000,’sheett’!$Q$10000:$Q$20000,1)”
So far I have been working on the formula string using split, mid, instr etc but it’s a lot of iterations to replace the row numbers from each seriescollection and keep the row reference.
Thanks for any good ideas
Comment from Jon Peltier
Time: Friday, September 23, 2011, 3:52 pm
Kenneth -
Go through twice, first changing 10000 to 20000, then changing 2 to 10001.
Comment from Kenneth
Time: Friday, September 23, 2011, 4:15 pm
Thanks, works fine if I know the chart range, and if the sheet name does not have the same number in it’s name.
I have written some code evaluating the each chartseries formula and replacing minimum row and maximum row.
Sub ChangeChartRowRange(minRow As String, maxRow As String)
Dim oldCF As String
Dim newCF As String
Dim X, Y
Dim ch As ChartObject
Dim s As Series
Dim t1 As String, t2 As String
Dim i As Integer, j As Integer
Dim shN As String
Dim R(1) As String
R(0) = minRow
R(1) = maxRow
For Each ch In ActiveSheet.ChartObjects
For Each s In ch.Chart.SeriesCollection
oldCF = s.Formula
X = Split(oldCF, “,”)
For i = 1 To 2
Y = Split(X(i), “:”)
shN = Left(Y(0), InStr(Y(0), “!”))
For j = 0 To UBound(Y)
t1 = Mid(Y(j), InStr(Y(j), “$”))
t2 = Mid(t1, InStr(2, t1, “$”) + 1)
Y(j) = WorksheetFunction.Substitute(t1, t2, R(j))
Next j
Y(0) = shN & Y(0)
X(i) = Join(Y, “:”)
Next i
newCF = Join(X, “,”)
s.Formula = newCF
‘Debug.Print oldCF
‘Debug.Print newCF
Next s
Next ch
End Sub
But I also found a class module written by Walkenbach, ChartSeries that writes and reads among several chartseries properties, the XValues and Values as a range. Not an array. Probably much more effective….
Comment from Jon Peltier
Time: Friday, September 23, 2011, 6:23 pm
You could use $10000 as the “find” string, and it will not match a number in a sheet name.
Comment from David
Time: Tuesday, October 25, 2011, 6:40 pm
Great Utility! Thank you!
I’ve tried to get this to work with R1C1 notation, and it has not worked. Does it work with R1C1 notation? Is there something I’m missing?
I tried to replace C13 with C12 and nothing happened.
Thanks again!
Comment from Jon Peltier
Time: Tuesday, October 25, 2011, 7:12 pm
David -
Good point. I rarely use R1C1 notation, so I don’t think of it. I’ll have a look when I get a chance.
Comment from Michael
Time: Friday, November 4, 2011, 6:23 am
Hi
What is the correct separator for one chart series using data series on different worksheets using Excel 2003?
This gives me an error
=’2011′!$B$8:$M$8+’2010′!$B$8:$M$8+’2009′!$B$8:$M8
as does this
=’2011′!$B$8:$M$8,’2010′!$B$8:$M$8,’2009′!$B$8:$M$8
Thanks,
Michael
Comment from Jon Peltier
Time: Friday, November 4, 2011, 8:07 am
Michael -
You can’t use data from different sheets this way. Your X Values, Y Values, and Series Name don’t need all to be on the same sheet, but each must be contained within one sheet.
Comment from Robert
Time: Friday, November 11, 2011, 7:05 am
Is it possible to change all charts on all sheets (ignoring sheets with no charts)?
Thanks
Comment from Alan
Time: Tuesday, December 20, 2011, 10:28 am
I have files built from scratch in Excel 2007 with many Stacked Area charts (1 per sheet) with as many as 25 series in each chart. I was updating these monthly, so you can imagine how long that was taking. Your add-in worked perfectly, so many, many thanks!!
Thanks, Alan
Comment from Lauren @ Sugar Coated Sisters
Time: Friday, December 23, 2011, 1:19 pm
Just wanted to say echo Alan’s sentiments above, such a time saver! Thank you!
Comment from Jim Komasinski
Time: Tuesday, January 3, 2012, 3:57 am
Here’s an interesting puzzle:
A series with constants for the 1st 11 y-values, but a cell reference in a worksheet
for a 12th y-value which is to be a variable value. Have not been able to get an error free syntax for this.
Anybody got the answer?
=SERIES(“Series5″,,{10000,20000,30000,40000,50000,60000,70000,80000,90000,100000,110000,Histories!M31},5)
Comment from Jon Peltier
Time: Tuesday, January 3, 2012, 9:31 am
Jim -
Excel doesn’t let you mix constant values and cell references within a single argument in the series formula. What you need to do is use another range, say Q1:Q12. Put your constants in Q1:Q11, and in Q12 put the formula =M31. Then use Histories!Q1:Q12 for your Y values.
Comment from David Rootes
Time: Sunday, January 22, 2012, 10:58 pm
Hi Jon, just tried this in 2010 as I have 120 charts I need to update, It works great on an individual chart, but when I tried to update about a dozen on the active sheet, it only did the first 3 and then destroyed the rest. Can you help?
Cheers, Dave.
Comment from Brandy
Time: Thursday, January 26, 2012, 1:04 am
This tool is amazing. Saving me loads of time, and I can’t express how much I appreciate you offering it! Thanks thanks thanks!






Write a comment
I welcome comments from my readers. If you have an opinion on this post, if you have a question or if there is anything to add, I want to hear from you. Whether you agree or disagree, please join the discussion.
If you want to include an image in your comment, post it on your own site or on one of the many free image sharing sites, and include a link in your comment. I'll download your image and insert the necessary html to display the image inline.
Read the PTS Blog Comment Policy.