Recorded Macro Error with Bubble and Stock Charts
by Jon Peltier
Thursday, January 28th, 2010
Peltier Technical Services, Inc., Copyright © 2010.
Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
You have a chart that you make every week or every month, and you’re tired of making it from scratch every time. So you record a macro to automate the process.
When you run the macro to make your chart the next time, you get no chart. Instead you get an ugly error message.

That’s frustrating. All you did was record and rerun a macro. You didn’t even change the code, so at least this once, you couldn’t have messed it up. And still it crashed.
Let’s examine the macro recorder code.
Sub RecordedMacro_MakeBubbleChart()
'
' RecordedMacro_MakeBubbleChart Macro
' Macro recorded 1/26/2010 by Jon Peltier
'
'
Charts.Add
ActiveChart.ChartType = xlBubble
ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("A1:C7"), _
PlotBy:=xlColumns
ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
End Sub
The macro recorder follow the order of the Excel 2003 chart wizard, putting the chart type before the chart source data. The steps given by the recorder work fine for most chart types, but a few chart types cannot be correctly assigned until the chart has been populated with sufficient data. In particular, code which creates stock charts and bubble charts will fail if you set the chart type before the chart is populated with data.
To make the code run properly without crashing, all you need to do is switch the SetSourceData and ChartType commands. The following code works fine.
Sub RecordedMacro_MakeBubbleChart()
'
' RecordedMacro_MakeBubbleChart Macro
' Macro recorded 1/26/2010 by Jon Peltier
'
'
Charts.Add
ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("A1:C7"), _
PlotBy:=xlColumns
ActiveChart.ChartType = xlBubble
ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
End Sub
This code isn’t particularly efficient, but at least it works. You can improve it following the suggestions in How To Fix a Recorded Macro.
This error has existed in Excel from the first modern version (Excel 97) through Excel 2003. Microsoft must have received inquiries about the problem, and they addressed it with a KB article, How to Use a Visual Basic Macro to Create a Bubble Chart., which propagated through versions 5/95, 97, 2000, 2002, and 2003. The author of the article didn’t think to rearrange the two VBA commands, so he wrote a long procedure that made an XY chart, and for markers, the chart used custom AutoShape circles sized according to the bubble values. It’s a nice procedure and a nice technique to know, but also a long voyage to go such a short distance.
When Microsoft rebuilt the charting infrastructure for Excel 2007, they changed some of the code produced by the macro recorder. Here is the Excel 2007 bubble chart macro:
Sub RecordedMacro_MakeBubbleChart()
'
' RecordedMacro_MakeBubbleChart Macro
' Macro recorded 1/26/2010 by Jon Peltier
'
'
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range("'Sheet1'!$A$1:$C$5")
ActiveChart.ChartType = xlBubble
End Sub
Excel 2007’s macro recorder has been criticized because it didn’t capture any actions that affected the new shapes which comprise the chart. But in the simple macro above, we see that some improvements were made to Excel 2007’s macro recorder:
- In earlier versions of Excel, a separate chart sheet is made, then later inserted in the worksheet. In 2007, a chart is directly added to the worksheet’s Shapes collection. That’s more efficient.
- More important, the chart type is not set until after the chart is populated with data. The error in recorded macros no longer occurs.
Related Posts:
- Make Your Recorded Macro Independent of Which Sheet is Active
- How To: Fix a Recorded Macro
- How To: Record Your Own Macro
- How To: Use Someone Else’s Macro
- Label Each Series in a Chart
- Dynamic Chart Source Data
- Book Review: Pro Excel 2007 VBA
- Label Last Point – Updated Add-In
- Quick VBA Routine: XY Chart with Axis Titles
- Extract Chart Data
Posted: Thursday, January 28th, 2010 under VBA.
Comments: 6
Comments
Comment from Colin Banfield
Time: Thursday, January 28, 2010, 8:11 am
Jon, despite what that KB article says, I don’t see how this error could have existed in Excel 5/95. Stock and Bubble charts were introduced in Excel 97.
Comment from Jon Peltier
Time: Thursday, January 28, 2010, 9:59 am
Colin -
Good point. They’ve been around for so long, I just assumed they went back to the Late Pleistocene era, when VBA was introduced. I’ve corrected the error.
Comment from Tony Rose
Time: Thursday, January 28, 2010, 10:18 am
Jon – This may be a little off topic, but still relevant. What would you suggest for someone who has pretty much mastered Excel except for any VBA. They can create great charts, dashboards and use just about any formula, but are not a programmer.
How does one start to learn VBA (resources, books, etc.)? Any suggestions or tips would be appreciated.
Comment from Jon Peltier
Time: Thursday, January 28, 2010, 2:55 pm
Tony -
Nothing about Excel is really off-topic here.
I wrote a series of “How-To” posts about VBA last year:
How To: Use Someone Else’s Macro
How To: Record Your Own Macro
How To: Fix a Recorded Macro
Make Your Recorded Macro Independent of Which Sheet is Active
How To: Assign a Macro to a Toolbar or Menu
How To: Assign a Macro to an ActiveX Control
How To: Assign a Macro to a Button or Shape
These should get anyone started, then Google can help once you know enough to guess at keywords.
Comment from Lee
Time: Tuesday, February 23, 2010, 10:36 pm
I noticed that excel 2007 will accept a picture from the clipoard as a plot area background. I have tried to record a Macro and searched the web to find the method associated with this action but haven’t had much luck. Do you have any insight? Is there a similar feature in 2003 that can be used.
Comment from Jon Peltier
Time: Wednesday, February 24, 2010, 8:38 am
Lee -
Excel 2010 includes the ability to record all kinds of charting actions. The only problem is, using the clipboard for the picture is not accurately recorded, and I can find no alternative in the object model documentation that will apply the contents of the clipboard to the fill of a chart element.


















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.