XL98: How to Create a Chart with Discontiguous Ranges (193248)



The information in this article applies to:

  • Microsoft Excel 98 Macintosh Edition

This article was previously published under Q193248

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. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to 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 requirements. 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.

Modification Type:MajorLast Reviewed:6/17/2005
Keywords:kbdtacode kbhowto KB193248