Microsoft KB Archive/107729
XL: Macro to Create a Bubble Chart
The information in this article applies to:
- Microsoft Excel for Windows, versions 3.0, 4.0, 4.0a
- Microsoft Excel for OS/2, version 3.0
- Microsoft Excel for the Macintosh, versions 3.0, 4.0
In 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.
If you are using Microsoft Excel 97 or Microsoft Excel 98 Macintosh Edition, you do not need to use a macro to create a bubble chart. These versions of Microsoft Excel include built-in support for bubble charts.
If you are using Microsoft Excel 5.0 or 7.0, please see the following article in the Microsoft Knowledge Base for a Visual Basic macro to create a bubble chart:
Q139662 : XL: How to Use a Visual Basic Macro to Create a Bubble Chart
Microsoft 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 professionals 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:
Creating the Macro
To create the macro, follow these steps:
- In a new macro sheet, type the following:
A1: Make Bubble A2: =ECHO(FALSE) A3: =MESSAGE(TRUE,"Gathering information........") A4: =GET.DOCUMENT(1) A5: =ACTIVATE?() A6: =GET.DOCUMENT(1) A7: =ACTIVATE(A4) A8: =SELECT(SELECTION()) A9: =MESSAGE(TRUE,"Creating the bubbles for chart.........") A10: =FOR("Count",1,COUNT(SELECTION())) A11: =ACTIVE.CELL()*50 A12: =ACTIVATE("Bubble.xlm") A13: =SELECT("1") A14: =DISPLAY(FALSE) A15: =FORMAT.SIZE(A11,A11) A16: =COPY() A17: =DISPLAY(TRUE) A18: =ACTIVATE(A6) A19: =SELECT("s1p"&Count) A20: =PASTE() A21: =ACTIVATE(A4) A22: =SELECT(,"RC") A23: =NEXT() A24: =ACTIVATE(A6) A25: =MESSAGE(TRUE,"One moment please.......") A26: =SELECT("") A27: =MESSAGE(TRUE) A28: =RETURN()
Explanation of Macro Code
A1: Name of the macro. A2: Disables screen updating. A3: Displays a message. A4: Returns the name of the active document. A5: Presents dialog box of all open documents. A6: Returns the name of the active document A7: Activates the document whose name is in cell A3. A8: Reselects the current selection. A9: Displays a message. A10: Sets up a FOR loop to iterate through the selection. A11: Calculates the width & height of circle for this point. A12: Activates the macro sheet. A13: Selects the circle that is on the macro sheet. A14: Displays the results of formulas. A15: Resizes the circle to the correct dimensions. A16: Copies the circle. A17: Restores the formulas to the cells. A18: Activates the chart. A19: Selects a point on the chart. A20: Pastes the circle on the point. A21: Activates the document whose name is in cell A3. A22: Activates the next cell in the selection. A23: Returns to the beginning of the loop. A24: Activates the chart. A25: Displays a message. A26: Deselects the final point in the chart. A27: Resets the status bar. A28: Ends the macro.
- In cell C1 of the macro sheet, create a circle by clicking the Oval tool (located on the Standard Toolbar in Microsoft Excel 3.0 and the Drawing Toolbar in Microsoft Excel 4.0).
- Hold down the SHIFT key and press the mouse button while you drag the insertion point to the right and down.
- Select cell A1 and choose Define Name from the Formula menu. Select the Command option to define the macro as a command macro and choose OK.
- Save the macro sheet as "BUBBLE.XLM" (without the quotation marks).
Using the Macro
To use the macro, follow these steps:
In a new worksheet in Microsoft Excel, enter the following values:
A1: B1: Gross Revenues C1: Net Income D1: # of Plants A2: East B2: 831191 C2: 35427 D2: 26 A3: West B3: 622199 C3: 54263 D3: 13 A4: North B4: 153794 C4: 80881 D4: 40 A5: South B5: 711327 C5: 33872 D5: 35
- Select B1:C5. From the File menu, choose New, select the Chart option and select the X-Values For XY-Chart option.
- In the chart window, choose Attach Text from the Chart menu, and select the Value (Y) Axis option to add labels to the y axis. Repeat this step for the x axis and for the four data points (to add labels to the data points, select the Series And Data Point option and specify the point number).
- Select the x axis label and in the formula bar, type
where FileName is the name of the worksheet that contains your data. Repeat this step four times so that the y axis label refers to cell $C$1 on the worksheet and the data points refer to cells $A$2, $A$3, $A$4, and $A$5 (respectively) on your worksheet.
The y value data labels will display the categories East, West, North and South, instead of the corresponding y values from C2:C5.
- Activate the worksheet and type the following formula in cell E2:
- Select cells E2:E5 and choose Fill Down from the Edit menu. This formula calculates the number of plants for each region relative to the total number of plants in all four regions.
- Select cells E2:E5 and from the Macro menu, choose Run. Select BUBBLE.XLM!Create_Bubble_Chart and choose OK.
- When the Activate dialog box is displayed, choose the name of the sheet that contains your xy (scatter) chart.
"User's Guide 1," version 4.0, Chapter 14
"User's Guide 2," version 4.0, Chapter 7
"User's Guide," version 3.0, Chapters 13 and 19
Additional query words: 4.00a
Keywords : kbmacro kbprg kbprb
Version : WINDOWS:3.0,4.0,4.0a; OS/2:3.0; MACINTOSH:3.0,4.0
Platform : MACINTOSH OS/2 WINDOWS
Issue type : kbhowto kbinfo
Last Reviewed: November 9, 1999