Change Series Formula – Improved Routines

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

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

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

Program to Modify All Charts on All Worksheets of the Active Workbook

Adding another loop to iterate through a sheet’s chart objects will change every chart on the sheet.

Sub ChangeSeriesFormulaAllChartsAllSheets()
''' Do all charts in all sheets
Dim oWksht As Worksheet
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 oWksht In ActiveWorkbook.Worksheets
For Each oChart In oWksht.ChartObjects
For Each mySrs In oChart.Chart.SeriesCollection
mySrs.Formula = WorksheetFunction.Substitute(mySrs.Formula, _
OldString, NewString)
Next
Next
Next
Else
MsgBox "Nothing to be replaced.", vbInformation, "Nothing Entered"
End If
End Sub

How the program works

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 inaccessibleIf 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

These new algorithms have been incorporated into the Change Series Formula function of the Peltier Tech Chart Utility, both Standard and Advanced Editions.

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!

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:

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?

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.

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.

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….

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!!

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.

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.

This program is really nice and helps a lot! (Sorry for writing on the wrong post in the first place). My question was, if you think this program could also function for the error bar series. This question was asked before but there was no reply. What do you think?

I found a “little” problem. I have my own set of procedures and they do work fine .. in most cases. However, recently I wanted to replace series formulas (chart copies pasted on another computer with different paths, so I could not open those originals) by new formulas:
eg. series:

I do not know whether it is a problem with formula length (over 256 chars), or commas in paths, file names or sheet names (I do not use commas in file name – this is “inherited” problem; a person who supplied the data created such names and I wanted to keep this original tree structure for easier comparisons).

I tried also .FormulaLocal (in Polish version it uses semicolons as separators, so should be less susceptible to comma-related problems, buy the result was exactly the same – missing Y data (perhaps it should not be a surprise, I presume that internally it all works with commas as separators and is later translated to a specific locale).

I tried it on a simpler case (shorter path/file/sheet strings, no commas) and it worked fine.

It is Excel 2002 sp3 (fully patched). I will try it on 2003 and on 2007 later.

So the problem is not changing path names but how Series().Formula works

AWM

PS. I usually use a couple of worksheets for kinetic data analysis etc., but once I update a sheet by adding some new plot (but leaving worksheet structure intact) I copy such plots to other/older worksheets and remove all file references (if the ws/sheet structures are the same) or change all chart data references to the current sheet. Those 2 kinds of chart manipulations are the most useful for me and my macros work fine.

The problem with partially empty chart series formulas (see my post above: long paths with commas – results in missing y-data) may be solved in some way by changing worksheet Links instead. It is much quicker than changing chart series one by one, but does not allow to change sheet and data range references (file/workseet references only)! However, it changes ALL referenes to a given worksheet – all charts, alll cell formulas – so you cannot restrict it to some charts or sheets. But… you may move your sheet (or copy chart) temporarily to a new empty worksheet, change links and move/copy back to the original location.

Here is an example of complete solution with comments etc. (it does not check path validity):

I’m sure my routine falls down because of the commas in the file names. I use a simple Split(string, “,”) to split the series formula at the commas. If the filename contains commas, then there are commas separating SERIES formula arguments and commas within SERIES formula arguments, and the Split command returns an array of gibberish.

Jon,
I do not use your routines and the problem is not *parsing strings* (if there is a problem, it is internal Excel/VBA problem), but the result given by Series.Formula() in VBA (partally empty). Anyway, I will try the same without commas but with the same path lenghts.

BTW. I myself follow the Way (i.e. Keep It Simple Stupid principle), however, sometimes one has to deal with data produced by someone who thinks in purely “human” not “computer” way.

Parsing strings would be problematic if the strings contained more commas than just those that separated the arguments. Since you use Excel’s Edit Links feature, so commas in the workbook names are not a problem.

Now that I reread your comments, I get what the problem is, missing arguments. ISTR this in the distant past, and if memory serves, it is probably related to length of the strings, and not on any special characters in those strings.

And yeah, sometimes it’s hard to instruct mere mortals to think in ways that make programming more efficient.

I downloaded your addin to change chart series and it worked great. However I have multiple worksheets with one chart per worksheets that I need to change the series range across all worksheets. Is this possible? i would prefer vba code rather than an add in.

Bubble charts are strange, with an extra argument in the SERIES formula, which isn’t treated exactly the same as a regular range reference. I never tested this program with bubble charts, so I don’t even know how strange their behavior is.

re. David Rootes’ issue with Excel 2010 charts being “destroyed” after only 3 were updated with the series change. I too experienced that issue. I found that if I scroll down past the last chart on the sheet BEFORE clicking on the “Change All Charts on Active Sheet” button, chart series were updated and no charts were “destroyed.” I have no idea why this worked.

Hi, This utility is a great idea unfortunately I get “Unknown Error Type Mismatch” when I use it with Excel 2007, I have a Scatter plot with 3 series –
=SERIES(“AWGN”,’B9’!$F$6:$F$10,’B9’!$I$6:$I$10,1)
=SERIES(“Rician, 12/200″,’B9’!$F$16:$F$20,’B9’!$I$16:$I$20,2)
=SERIES(“Rician 9/10″,’B9’!$F$26:$F$30,’B9’!$I$26:$I$30,3)
and trying to replace $F$ with L
Not sure what I am doing wrong. Please advise. Thanks in advance!

My algorithm interprets the formula by splitting the series formula into its component parts at the commas in the formula. It isn’t smart enough to know that the comma in “Rician, 12/200″ is in the middle of the series name, so it breaks when it tries to interpret 12/200 as the X values.

Thank You so Much for the chart formula utility! I can’t thank you enough!

Background Story:

I am a disabled ‘Nam Era USMC vet who works as a volunteer at the local VA hospital.

I can tell you that I have undertaken developing a means of tracking and graphing items between the main VA hospital , and 10 satellite hubs in the state in monthly and yearly charts. I ended up with sovereign data (after a few iterations), but needed 840 charts to display all the data on 10 worksheets!

Needless to say changing chart references was genuinely, seriously, painful and time consuming to the extreme.

This utility probably saved me four full days worth of work manually editing the formulas…

I feel that Excel 2010’s charting functions are worse off than in 2003 version. I have used this technique sucessfully for years, now, I have defined my ranges, both x and y, but the Chart series function gives me an error, “Function is not valid” yet it actually locates the correct data set in my lookup table. HELP

Hi,
I have Excel 2010 and one, two or four series per chart. This add-in works like a chart for charts with only one series, but not for those with two series (haven’t tried with four, yet) Excel crashes and is forced to close.
Any updates for this problem? I would find this VERY USEFUL in my data analysis…

Marko –
This works fine on the multi-series charts I use all the time.
What chart type are you using?
Reply to this thread, and include the two series formulas for a chart that can’t be updated without crashing.

I have several pages of several complex graphs containing multiple data series. The dead line is HOT!
If only I could set up a page and copy it. But, all the links point to the original data. It’s mind numbing pain to hunt down each reference and change it by hand.

Well, now I CAN copy! This should have been a feature in Excel.

I’ll sleep better tonight and wanted to thank you from the bottom of my heart!

This is exactly what I have been looking for! I can’t get the add on to work for me though it keeps saying it can not find the macro. I still wanted to use it though so I used your VBA code and I still got an error on the

Using the code in your own project: Is mySrs properly declared and assigned to a series? What is mySrs.Formula before this line? What are OldString and NewString? What is the result of the Worksheet.Substitute operation?

Hi Jon,
The Change Series Formula Utility does seem to be working in Excel 2013 but only when the Add-Ins Tab is visible at the top of my worksheet. When I open a file and then do file open and go back and double click the file PTS_ChangeSeriesFormula.xla that I have downloaded and saved under /AppData/Roaming/Microsoft/Addins/, the Add-ins tab appears at the top of my Excel sheet and the add-in works. I save and close the file. When I reopen the same file or create a new file or open a different file the Add-Ins Tab is no longer at the top of my screen. I must go back to Change Series Formula Utility file and double click it every time so that the Add-in Tab appears at the top of my screen for the add-in to work. Note that when I go to File Options Add-Ins the Change Series Formula Add-In is correctly listed as an Excel Add-in, and the Customized Ribbon has Add-ins correctly selected.

Saving a file in the blahblah\Add-Ins directory only makes it available to be installed, but does not install it.
In Excel, go to the add-ins dialog (Office button or File tab > Options > Add-Ins > Go) and check the box in front of the add-in’s name. If the add-in is not shown in the list, click Browse, navigate to the file, and click OK a couple times to return to Excel.
Now the add-in will be always available.

The box in front of the add-in Change Series Formula Add-In is listed and it is checked. However, it is not available unless I double click the add-in file where it is saved in order to get the Add-Ins button to appear at the top my Excel worsheet for the add-in to work.

I got it to work properly in my own program. I don’t know how but it works now and it is great! Thank you so much for replying so quickly and for giving this program out to use! It is amazing and saves so much at work! I can’t thank you enough!

Jon,
This is such a time saver! I have a workbook with four charts on each tab and about 50 tabs, I just need to change one thing in every chart so part of the name would go from May to June. Is it possible to modify this so it would change all the charts in the workbook?

In the “ChangeSeriesFormulaAllCharts” routine:
1. Declare this variable
Dim oWksht As Worksheet
2. change this one line
For Each oChart In ActiveSheet.ChartObjects
to these two lines
For Each oWksht In ActiveWorkbook.Worksheets
For Each oChart In oWksht.ChartObjects
3. Before “Else”, insert one more “Next”

Rick –
I don’t know what’s not working. The checkbox tells you the file is open, but the lack of buttons on the Add-Ins tab indicates that it is not. Try this:
Uninstall the utility (uncheck the box), move the file to a different directory, then reinstall it from the new directory (using Browse to locate the file).

Hi Jon,
As suggested I uninstalled the utility (unchecked the box), moved the file to a different directory, and then reinstalled it from the new directory (using Browse to locate the file). Unfortunately It still does not work in Excel 2013. I then tried it in Excel 2010 and it works fine but not in 2013 which still does not display the add-in button in the toolbar unless the file is double clicked each time you want to use it in order to activate the add-in. Is it possible that xla files work in 2010 but not 2013 and that only xlam files work in 2013? I also played with the macro settings under the Trust Center in 2013 and it did not help. Note that I do not have the entire 2013 suite installed (I only have Excel 2013 the rest of my programs are 2010) and I have both the 2010 and 2013 versions of Excel installed. I don’t know if that makes a difference.

Rick –
The behavior you describe is not normal. Both .xla and .xlam files work the same in Excel 2013 (and 2010 and 2007). It should not matter which pieces of which version of Office are installed, or that multiple versions of Excel are installed. Once installed, the add-in should be available and its buttons should be visible, without having to also open the add-in file.
I wonder whether reinstalling Excel 2013 would fix the problem.

Ran this, and it worked great!!!
Now I’ve added data and need to run it again but I’m getting an error message:
Run time error 1004:
Application defined or object defined error
selecting debug points to this line in the VBA from what I pasted above…
mySrs.Formula = WorksheetFunction.Substitute(mySrs.Formula, OldString

Ken –
Hover the cursor over the arguments in the Substitute function. Are mySrs.Formula, OldString, and NewString correct? Type this into the Immediate Window and press Enter:
?WorksheetFunction.Substitute(mySrs.Formula, OldString, NewString)
What is the result? Is it a valid series formula? Does it match what you get if you manually change the data range?

Jon,
Regarding Ken’s comment: I am getting the same error. Run time error 1004:
Application defined or object defined error
Which is pointing to the mySrs.Formula line. I checked my results by typing ?WorksheetFunction.Substitute(mySrs.Formula, OldString, NewString) and the result was correct. I am completely stumped. The code worked earlier, but now it crashes. Any further insights?

Here is the code I have right now which I have adapted to work on all charts for all worksheets and I have the code replacing a very specific section as my series are all values so no need for the input box:

Sub ChangeSeriesFormulaAllCharts()
''' Do all charts in the whole workbook
Dim oWksht As Worksheet
Dim oChart As ChartObject
Dim OldString As String, NewString As String
Dim mySrs As Series
OldString = ",{0"
If Len(OldString) > 1 Then
NewString = ",{#N/A"
For Each oWksht In ActiveWorkbook.Worksheets
For Each oChart In oWksht.ChartObjects
For Each mySrs In oChart.Chart.SeriesCollection
mySrs.Formula = WorksheetFunction.Substitute(mySrs.Formula, _
OldString, NewString)
Next
Next
Next
End If
End Sub

This seems like the issue should be jumping out at me, but I can’t figure out the issue. The dimensions for each variable are correct and the code is looking for series in each of the charts which seems straigh forward. I am really confused.

Okay, I managed to figure out what is causing the issue. Ignore the parenthesis in the examples below. What I am working with is inside the parenthesis.

If there are any number of charts on a sheet that have something I want to replace, for example ( {0, ) with ( {#N/A, ) then there is no issue. The code fails if there is a chart that does not have ( {0, ). So, all charts must have what I am looking to replace. The code worked with the dialogue boxes (as you wrote it) and with my modifications. The only issue is if there is a chart that does not have what I am looking to replace.

Thanks for the quick response. Not sure if there is a work around for this problem, but I’ll keep working on it.

Actually, it looks like the problem is something different. I am able to run the code if charts do not contain what I’m looking for. It seem to be hung up on three charts and I’m not sure what is different about these charts that is causing the problem. There are 27 charts and when I delete the three that are causing the problem, the code works perfect. I am hoping you can help me understand what is different between the series that work and the three that are not working. Keep in mind, the three are not right next to each other; the first is in the middle of all the charts and the last two are the last two charts in my workbook. I have tried running the code on individual sheets and the same charts cause the problem every time. Here is a series of a chart that works:

Sorry for the multiple post, but I think I finally have it figured out. Here is how and when the code fails:

The code checks for all of the series in each chart, therefore, the problem was within series number 1 (which I wasn’t even concerning myself with). I was askig the code to check for all instances of {0 and replace that with {#N/A. So here is the rub, if I have a series that has this combination:

I have posted this on the http://answers.microsoft.com site but I thought I would post this other places just to see if anyone is familiar with this issue.

I have a very peculiar issue with series in charts that have been converted to values once links are broken. I cannot see a discernable pattern of numbers that cause the issue; however, it is very easy to recreate. There needs to be a workbook that contains data and a workbook that contains the chart. On the workbook that contains data, enter in 0.29 for any number of periods, let’s say a total of ten. All periods would be the same number; 0.29.

Now create a line chart with markers in a different workbook and link to that set of numbers (the 0.29 set) and save this workbook. Now there should be two workbooks and the one with the chart is linking to the one with the data. Break the links and save the file with a new name. Clicking on the chart’s series will reveal something that looks like this:

This appears to be normal and everything is working fine. Now close the two files which will end this instance of Excel and clear all memory. Next, open the file that is not linked and once the series is selected, the error is obvious. This is what the series looks like after the instance has been restarted:

Two things. 1. The error is within the action of breaking links of a parent worksheet and restarting the Excel instance. So, there are a number of items Excel charts do not like in this case. For example, some numbers do not display correctly (exactly as you mentioned regarding the binary issue), if the chart was linked to a range of cells that had a blank cell (this would normally be treated as zero but once links are broken there is a #QNAN error), same goes for #NA.

2. The links were in a range of cells in the parent workbook; however, those links must be broken before the file is sent out to others. It is this action of breaking links that causes the problem. The only solution I can think of is to have the values for the charts link to a hidden sheet so that one links are broken, the cells on the hidden sheet convert to values and the charts’ integrety are maintained. The problem with this is all the VBA code I have written for the process. There are thousands of lines of code across multiple modules all designed with the intention of updating a lot of charts. So you can imagine I’m not to keen on this idea.

Hi,
I am working in several barcharts on the same sheet and workbook and was wondering if the code you gave in the section “Program to Modify All Charts on the Active Sheet” is workinng if I want to change the text format in the y axis.

For example, if I have to put a space between the data and the percentage sign for all of them, is this program helpful?

Audrey –
The number format is not part of the series formula. However, it’s easy to adjust. Format the legend, and insert the space right into the number format (you may have to define a new custom number format). In other words, change the number format from 0% to 0 %.

Dominick –
The thing with Excel charts, is that they work best with worksheet ranges. Especially if there is anything non-numeric in the data range.
If you had hidden (or not hidden) sheets that drove the charts, and these sheets linked to data in other workbooks, it would be rather easy to simply convert the cells to their values, and all charts downstream wouldn’t even notice. I’m sure the code would be simpler, and you wouldn’t have to be removing sheets.
Have you considered replacing the charts with pictures of the original charts? Not formattable, but maybe that’s fine.

I agree with your point and the easiest way to solve my problem is to link the chart data to hidden sheets; however, as I mentioned before, that would involve a ton of code to be rewritten. Making the charts pictures would normally be the best choice; however I need the charts to remain scalable for use in other reports or applications like PowerPoint. At this point I did get a response from Microsoft acknoleding the bug but no word on a fix. See, even if I was to include only numerical values, the charts still have floating point issues. As in my example, just linking to the number 0.29 causes the series in the chart to get whacky. At this point it i faster for me to manually fix any errors that occure after my code has run as this is just a monthly report. Knowing what I do now, I will be mindful of how data is linked to charts in the future to avoid this problem. Bummer though.

Thanks so much for the post. I am just starting to learn VBA’s so sorry for the beginner question. I have slides with upwards of 40 scatter plots. Is it possible with the macro to select multiple graphs and have that be the Active Chart. I don’t want all 40 to be updated with the new range.

Hi Edward-
The utility at the end of the article has this capability. Depending on what’s selected, you have a choice to do the active chart (if one chart is selected), all selected charts (if multiple charts are selected), or all charts on the worksheet (if not all charts are selected).

I can’t get it to change anything. Using Excel 2003. I then tried your ChangeSeriesFormula, and it also doesn’t replace anything.
When it executes ” mySrs.Formula = strTemp”, it does not actually change anything in the sheet.
Here’s what I see. I tried to change “Sp500″ to “none”.
Watch : : mySrs.Formula : “=SERIES(“W1″,,'[Sp500 lowpe.xls]Staggered’!$AR$6:$AR$188,1)” : String : Sheet12.ChangeSeriesFormula

It didn’t put the change into the spreadsheet. When I go back and look at the Values in SourceData/Series, it is still Sp500.

Ah-ha! Found it. It won’t change just part of a string inside the single quotes. Excel says the series is: ='[Sp500 lowpe.xls]Staggered’!$BE$6:$BE$188. I was trying to change 500 to none.
What I had to do was change ‘[Sp500 lowpe.xls]Staggered’ — that is, the old string is everything inside the single quotes, including the quotes.

When I used the Change Series function, I set oldstring to ‘[Sp500 lowpe.xls]Staggered’ and newstring to Staggered. And it worked.

The code will change part of the string inside quotes. You just have to specify your strings thoughtfully.

You could have entered [Sp500 lowpe.xls] in the Change What box, and left the Change To box blank. If you put the word none anywhere, Excel treats it as one of the strings you’re using, it doesn’t know that “none” means nothing.

I downloaded the Add-In and it works great. I’m trying to take the code you gave to be able to edit multiple charts in one workbook but I confess my VBA experience is one college class many years ago.

I’m trying to do this:

In the “ChangeSeriesFormulaAllCharts” routine:
1. Declare this variable
Dim oWksht As Worksheet
2. change this one line
For Each oChart In ActiveSheet.ChartObjects
to these two lines
For Each oWksht In ActiveWorkbook.Worksheets
For Each oChart In oWksht.ChartObjects
3. Before “Else”, insert one more “Next”

I’m not seeing where oChart was declared as a variable in the original code – do I need to do that? I’m also not seeing where “For Each oChart….” (the line that needs to be replaced) is located.

Even if I never get this part to work you’ve already saved me a ton of time so it’s no biggie. I was just trying to increase my VBA skills a little. Sorry if this question is completely ridiculous.

Kat –
I’ve added another procedure called ChangeSeriesFormulaAllChartsAllSheets. I should probably have included it in the original article. The declarations, as always, are at the top of the procedure.

“Now I’ve added data and need to run it again but I’m getting an error message:
Run time error 1004:
Application defined or object defined error
selecting debug points to this line in the VBA from what I pasted above…
mySrs.Formula = WorksheetFunction.Substitute(mySrs.Formula, OldString”

Your reply was:

“Ken –
Hover the cursor over the arguments in the Substitute function. Are mySrs.Formula, OldString, and NewString correct? Type this into the Immediate Window and press Enter:
?WorksheetFunction.Substitute(mySrs.Formula, OldString, NewString)
What is the result? Is it a valid series formula? Does it match what you get if you manually change the data range?”

What window should I paste this into? Basically what I’m trying to do is change all of the series with CZ to DA. I’m compiling charts that stay updated on a 12 month basis and DA is the beginning of the last 12 months. This worked fine with the other macro, updating each chart individually, but I’ve got almost 50 charts so you can see why it would be better to get all of them at once :)

The Immediate Window is part of the VB Editor. Press Ctrl+G to show this window. It is a tremendous way to interact with VBA.
Keep in mind that series formulas include not just the cell addresses, but also the worksheet names. You may have to change these as well if the data resides on individual sheets.

Do you have any tips/tricks for using ActiveX Scroll Bars (sliders) for changing series data? I’m trying to simulate an oscilloscope using an Excel Chart. I searched your website (and the world) and can’t find any help.

This is a great trick. I use this VBA all the time. Is it possible to strike a middle ground between 1 chart and all charts. I want to modify 10 graphs on a chart on the same time when they are selected. Could you please advise?

I am using trying to change the worksheet name reference on an XY plot. The add-in works great for changing the X value portion, but does not change the name or the Y value references. How can I get these to change as well?
Thanks
Trevor

Jon
Here is one of the series equations:
=SERIES(Master!N_04,Master!X_04,Master!Y_04,1)
I would like to replace the Master! with Week_42! and do this for 25 similar series equations.
I am not sure what I was doing different yesterday, but today none of the portions (series name, x data, y data) are changing.
Thanks
Trevor

Tim –
If the sheet names are in fact Sheet1 and Sheet2, then OldString is Sheet1 and NewString is Sheet2. If they are different, OldString is the old sheet name with single quotes if the sheet name is surrounded by single quotes in the series formula; NewString is the new sheet name surrounded with single quotes (if they are not needed, Excel will ignore them).

Jon, the first 4 sheets in my workbook have about 40 charts each. Your code was perfect to enable updating all of the data series in each chart on each sheet, but sometimes (like on sheets 2, 3 and 4 but not on sheet 1) the plot areas get moved so the charts end up jumbled together. Has anyone else run into this? If so, any suggestions on how to either keep this from happening or, using VBA, put the plot areas back in their original position? Thanks.

Jon, I’m using Excel 2010. The charts are mostly line charts but there are a few other types (area, stacked bar, combination line/bar, combination line/area.) The funny thing is, when the plot areas get moved, they get moved for all chart types on a given sheet. Meanwhile, on another sheet, nothing gets moved. By the way, based on my Googling, no one else has seen this either.
I wish I had something to show you, but visualize, for example, the left edge of the plot area in Column AT before the routine runs, and Column AP afterwards, and the top edge in Row 4 before and Row 3 after. Since I don’t think I’ll be able to figure this out, is there a VBA way to select all plot areas and reposition them en masse?
Anyway, thanks for your response.

Well, problem solved, and of course it was something seemingly unrelated to what I thought I was doing. I’m not sure if this has general applicability, but I thought I’d close the loop anyway – the zoom factor on the sheets that were getting jumbled was greater than 100%. When I reset it to 100% before running the macro (needless to say, I didn’t create the workbook with all of the charts, just the macro to automate updating the data series), everything came out fine. Thanks again for your code which saves us countless hours every month.

I have an error when changing from column name with two letters to a column name with one letter. I’m not sure if other people are having this problem but this is what is occurring for me. I used the code for modifying the series for a selected chart (first code snippet). If I do something like $d$ to c it works fine. If I do $cm$ to $ba$ it works. When i try to do $cm$ to d all of the series are deleted from the chart. Is this a known error? Help would be appreciated.

Using ChangeSeriesFormula under the section “Program to Modify the Active Chart”, I changed a series from $AF$ to c and from $C$ to af repeatedly, no problems. Do all of the ranges contain data? Are the series actually deleted, or are they pointed to blank ranges? What are the before and after series formulas?

Thank you for the reply. I found the issue and it was NOT in your macro. In fact I observed the same behavior even when I didn’t use the macro. The series I was changing referenced a data column. The column to which I was changing the series to was hidden in that tab. Whenever I changed the reference to a hidden column the data series disappeared! Is this a known bug. When I unhid the column the data series re-appeared. Perhaps I am just a noob but I had no idea that this happened.

On a separate note, I have text boxes on these chart which fill with the statistics for a formula (using the text box linked to a cell). Is there any way to do a VB script to change these references like for the chart? I can ask this question in another place if more appropriate. Thanks again.

That’s not a bug. That’s Excel’s default for plotting of hidden data. To change this for a chart, select the chart, click the Select Data button on the ribbon, then click the Hidden and Empty Cells button in the bottom left of the dialog. Check Show data in hidden rows and columns, and click OK a couple times to get back to Excel.

I found the error, although, I can’t explain it. I was trying to have my charts changed to point to a different sheet. The new sheet had a space in it’s name. When I removed the space, the error disappeared, and the routine ran perfectly. Thanks.

A more complicated algorithm is required if the sheet name being changed from or to contains a space, hyphen, or certain other characters. I didn’t include the algorithm here but it’s built into my Chart Utility.

Hello. This is awesome. A question though: I have a different chart for every row. So, while this saves me time by not having to change each series within each chart, I still have to run the macro for each chart. It’d be really cool, if you could match the chart title to the row and make the replacement of that criteria. Not sure if that makes sense. Let’s say I have 200 rows, and each row is a chart. When I create the first chart, it will be five series from row 1 (5 years across columns). The next chart will be five series from row 2. Obviously, I would copy and paste to create the chart, but I’d have to run the macro on each of these charts. Not sure if there is a better way. Thanks.

Trackbacks

[…] dynamic, there is a need to constantly update the summary workbook. The idea was to use a code from peltiertiertech utility to do the job. when assigning values directly to the old and new strings I want to swap. It […]