XL98: How to Create a Chart with Discontiguous Ranges

ID: Q193248

The information in this article applies to:

SUMMARY

When you record a macro to create a chart using discontiguous ranges, the source address of cells used to create the chart is fixed by Microsoft Excel. This article contains a sample Visual Basic for Applications macro that you can use to create a chart when the ranges containing the data to be used in your chart are discontiguous.

MORE INFORMATION

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:

   http://www.microsoft.com/support/supportnet/refguide/

Follow these steps to create the sample macro:

1. Enter the following information in Sheet1 in a new Workbook:

       C3:          D3:  Region 1   E3:  Region 2   F3:  Region 3
       C4:  Jan     D4:  10         E4:  80         F4:  15
       C5:  Feb     D5:  20         E5:  70         F5:  25
       C6:  Mar     D6:  30         E6:  60         F6:  35
       C7:  Apr     D7:  40         E7:  50         F7:  45

      C12:  Jan    D12:  10        E12:  80        F12:  15
      C13:  Feb    D13:  20        E13:  70        F13:  25
      C14:  Mar    D14:  30        E14:  60        F14:  35
      C15:  Apr    D15:  40        E15:  50        F15:  45

2. Start the Visual Basic Editor (press OPTION+F11).

3. Click Module on the Insert menu.

4. Type the following code into the module sheet:

      Sub CreateChart()

         Dim range1 As Range, range2 As Range, ChartRange As Range

         ' Set variable to contents of the first region to be charted.
         Set range1 = Range("c4").CurrentRegion

         ' Set variable to contents of the second region to be charted.
         Set range2 = Range("c12").CurrentRegion

         ' Set variable to the entire region to be charted.
         Set ChartRange = Union(range1, range2)

         ' Add a new chart to the worksheet.
         Charts.Add

         With ActiveChart

            ' Set the type of chart.
            .ChartType = xlLineMarkers

            ' Set the source range for the chart.
            .SourceData Source:=ChartRange

            .Location Where:=XlLocationAsObject, Name:="Sheet1"
         End With
       End Sub

5. Run the macro.

A new chart wil be created on Sheet1.

Additional query words: XL98

Keywords          : kbdta kbdtacode xlvbahowto xlvbainfo 
Version           : MACINTOSH:98
Platform          : MACINTOSH
Issue type        : kbhowto

Last Reviewed: May 18, 1999