XL: How to Use a Visual Basic Macro to Create a Bubble ChartLast reviewed: December 1, 1997Article ID: Q139662 |
The information in this article applies to:
SUMMARYIn Microsoft Excel, you can write a macro to create a bubble chart. Bubble charts are similar to xy (scatter) charts; however, instead of having uniform markers where the pair of x and y values intersect, these points are marked with circles that indicate the relative magnitude of the values in a third series.
MORE INFORMATIONMicrosoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:
http://www.microsoft.com/support/supportnet/refguide/default.aspEnter the following code on a module sheet and follow the instructions below:
Sub BubbleChart()
'define variables for workbook, chart, and chart series
MyBook = ActiveWorkbook.Name
MyChart = ActiveChart.Name
MySeries = ActiveChart.SeriesCollection(1).Formula
'define variables for worksheet and chart series reference
StartVal = InStr(InStr(1, MySeries, "(") + 1, MySeries, ",") + 1
EndSheetVal = InStr(StartVal, MySeries, "!")
mysheet = Mid(MySeries, StartVal, EndSheetVal - StartVal)
EndVal = InStr(StartVal, MySeries, ",")
mysource = Mid(MySeries, StartVal, EndVal - StartVal)
If InStr(mysheet,"'") Then 'strip out apostrophe
mysheet = Mid(mysheet, 2, Len(mysheet) - 2) 'if sheet name has a
End If 'space
'begin loop to add data labels to chart
Counter = 1
For Each xItem In Range(mysource)
xLabel = xItem.Offset(0, -1).Value
ActiveChart.SeriesCollection(1).Points(Counter).HasDataLabel _
=True
ActiveChart.SeriesCollection(1).Points(Counter).DataLabel.Text _
=xLabel
Counter = Counter + 1
Next xItem
'create oval on worksheet (used for chart bubbles)
Workbooks(MyBook).Sheets(mysheet).Activate
ActiveSheet.Ovals.Add(335.25, 12.75, 52.5, 52.5).Select
Application.ScreenUpdating = False
MyOval = ActiveSheet.DrawingObjects.Name
'get values from worksheet to compute bubble sizes
Set MyBubbleRange = Range(mysource).Offset(0, 3)
'begin loop to compute bubble size and add to chart data point
For Counter = 1 To MyBubbleRange.Count
BubbleValue = MyBubbleRange(Counter) * 50
ActiveSheet.DrawingObjects(MyOval).Select
With Selection
.Width = BubbleValue
.Height = BubbleValue
End With
Selection.Copy
Workbooks(MyBook).Sheets(MyChart).Activate
ActiveChart.SeriesCollection(1).Points(Counter).Select
Selection.Paste
'select worksheet
MyBubbleRange.Parent.Activate
Next Counter
'activate chartsheet
ActiveWorkbook.Sheets(MyChart).Activate
'remove oval from worksheet
ActiveWorkbook.Sheets(mysheet).DrawingObjects(MyOval).Delete
End Sub
To use the macro
ARTICLE-ID: Q107729 TITLE : Excel: Macro to Create Bubble Chart |
Additional query words: 5.00 7.00 bubbles circles
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |