Using a macro to add chart series

pksmith

New Member
Joined
Aug 23, 2015
Messages
10
Hello all,

I am trying to write a macro that creates an X/Y scatter chart. The series will be named ranges (ex: Series X Values ='GraphTool.xlsm'!XValues1'; Series Y Values ='Graph Tool.xlsm'!YValuesTemp1".

I have several named ranges that I plan to use a for loop to insert them as series as needed (ex. several ranges that will be X values, several that will be Y values)

I would like to know how I create a new chart and add series using the named ranges in a macro.

Thank you for your help!
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi

This is an example that creates a XY chart in Sheet1, then adds 2 series using named ranges in Sheet1.

For this test I used the named ranges in Sheet1:

XValues1 =Sheet1!$B$4:$B$7
XValues2 =Sheet1!$E$4:$E$9
YValues1 =Sheet1!$C$4:$C$7
Yvalues2 =Sheet1!$F$4:$F$9

I placed the chart over the range D12:M25

Try:

Code:
Sub CreateChartXY()
Dim ws As Worksheet
Dim chto As ChartObject, cht As Chart
Dim rChart As Range

Set ws = Worksheets("Sheet1")

' add the chart
Set rChart = ws.Range("D12:M25") ' range that the chart overlaps
With rChart
    Set chto = ws.ChartObjects.Add(.Left, .Top, .Width, .Height)
End With
chto.Name = "MyChart"
Set cht = chto.Chart

' adds the series
With cht

    ' set the chart type
    .ChartType = xlXYScatter

    ' add the first series
    
    With .SeriesCollection.NewSeries
        .Name = "Series 1"
        .XValues = ws.Range("XValues1")
        .Values = ws.Range("YValues1")
    End With

    ' add the second series
    
    With .SeriesCollection.NewSeries
        .Name = "Series 2"
        .XValues = ws.Range("XValues2")
        .Values = ws.Range("YValues2")
    End With

End With

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,334
Messages
6,124,323
Members
449,154
Latest member
pollardxlsm

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top