Peltier Tech Blog

Excel Chart Add-Ins | Training | Charts and Tutorials | Peltier Tech Blog

 

Main menu:

Peltier Tech Chart Utilities for Excel Peltier Tech Panel Chart Utility Peltier Tech Waterfall Chart Utility Peltier Tech Cluster-Stack Chart Utility Peltier Tech Box and Whisker Chart Utility Peltier Tech Marimekko Chart Utility Peltier Tech Dot Plot Utility Peltier Tech Cascade Chart Utility

 
Excel Dashboards
 

 
Amazon Books
 

Subscribe

Site search

Subscribe

Site search


Recent Posts

Popular Posts

Privacy and License

Privacy Policy

Creative Commons License
Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.

Recorded Macro Error with Bubble and Stock Charts

 
by Jon Peltier
Thursday, January 28th, 2010
Peltier Technical Services, Inc., Copyright © 2012.
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.

Chart Type VBA Error

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:

Learn how to create Excel dashboards.

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.


Comment from Stock Charts
Time: Tuesday, June 22, 2010, 7:00 am

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, June 23, 2010, 9:56 am

In 2003 you can use an image file as the background fill for chart and plot areas and fills for series that use fills (area, bar, column). This action should record in 2003 and 2007. The 2010 recorder has a lot of the stuff that 2007′s didn’t get, so perhaps recording the “fill from clipboard” action in 2010 will give code you can use in 2007.


Comment from Dalph
Time: Tuesday, September 21, 2010, 8:01 am

Hi Jon, your site has saved me many times in the past.

We recently switched to 2007 at work, and I am facing the following issue.

I am creating reports on 4 brands.
In a spreadsheet, I have a list of logos populating pictures (all is linked)
I have created bubble charts to represent scores of the brands across various dimensions/ statements.

So far, all fine. Now, the issue. I have tried the following : go to the Logo spreadsheet, copy picture 1, go back to my chart, select SeriesCollection1, format, fill with pic from clipboard. Fantastic ! It works.
Now considering I have about 50 bubbles, I’d like to kind of automate this process.

Any suggestion or part of code that could help me ? I have no possiblitiy to either go back to 2003 or run 2010.

Thanks in advance
Dalph


Comment from Jon Peltier
Time: Tuesday, September 21, 2010, 9:35 am

Hi Dalph -

I recorded a macro in 2003, then severely edited it:

Sub UseLogosForMarkers()
    ActiveSheet.Shapes("Logo1").Copy
    ActiveSheet.ChartObjects("MyChart").Chart.SeriesCollection(1).Paste
    ActiveSheet.Shapes("Logo2").Copy
    ActiveSheet.ChartObjects("MyChart").Chart.SeriesCollection(2).Paste
End Sub


Comment from Dalph
Time: Tuesday, September 21, 2010, 10:00 am

Thanks Jon, this works perfectly !!! Now I need to modify the code sor that it can run across brands and statements … Will be fun once finished.

But why the hell have microsoft modified the “recorder” ? …


Comment from Jon Peltier
Time: Tuesday, September 21, 2010, 10:06 am

Dalph -

They didn’t modify the recorder. They completely revamped the shapes used in Office, and the chart pieces that are comprised of these new shapes. They did not have the time or resources to update the recorder to incorporate these changes. Fortunately the Excel 2010 has been updated. Unfortunately, the help files follow a trend started in about Office 2002, in that they are increasingly less helpful.

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.





Subscribe without commenting

Peltier Tech Chart Utilities for Excel Peltier Tech Waterfall Chart Utility Peltier Tech Box and Whisker Chart Utility Peltier Tech Cluster-Stack Chart Utility Peltier Tech Panel Chart Utility Peltier Tech Marimekko Chart Utility Peltier Tech Dot Plot Utility Peltier Tech Cascade Chart Utility

Create Excel dashboards quickly with Plug-N-Play reports.