+ Reply to Thread
Results 1 to 9 of 9

Adding multiple new series to a Scatter chart

Hybrid View

  1. #1
    Registered User
    Join Date
    05-28-2012
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003
    Posts
    5

    Adding multiple new series to a Scatter chart

    Hi All,
    I am attempting to create a macro to add multiple series to a scatter chart. I have a range for both the X and Y Values (see attached).
    At present, I have been manually adding series one by one to the chart, but I have roughly a hundred series to add so this could get tiresome.
    I am fairly new to coding/VBA so your help would be much appreciated.

    Cheers,
    Matt

    TEST.xlsx

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,461

    Re: Adding multiple new series to a Scatter chart

    try this,

    Sub CreateChart()
    
        Dim RowIndex As Long
        Dim XData As Range
        Dim YData As Range
        Dim NameData As Range
        Dim Cht As Chart
        
        
        ' reference data
        Set NameData = Range("A3", Range("A3").End(xlDown))
        Set YData = NameData.Offset(0, 1).Resize(NameData.Rows.Count, 3)
        Set XData = YData.Offset(0, 3)
        
        ' get chart and remove existing data
        Set Cht = ActiveSheet.ChartObjects(1).Chart
        With Cht
            Do While .SeriesCollection.Count > 0
                .SeriesCollection(1).Delete
            Loop
        
            ' add rows of data
            For RowIndex = 1 To NameData.Rows.Count
                With .SeriesCollection.NewSeries
                    .Name = "='" & NameData.Parent.Name & "'!" & NameData.Cells(RowIndex, 1).Address(xlR1C1)
                    .Values = "='" & YData.Parent.Name & "'!" & YData.Rows(RowIndex).Address(xlR1C1)
                    .XValues = "='" & XData.Parent.Name & "'!" & XData.Rows(RowIndex).Address(xlR1C1)
                    With .Format.Line
                        .EndArrowheadStyle = msoArrowheadTriangle
                        .ForeColor.RGB = 5296274 ' green
                        .Weight = 1
                    End With
                    .Smooth = True
                End With
            Next
            .HasTitle = False
        End With
        
    End Sub
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    05-28-2012
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Adding multiple new series to a Scatter chart

    This is great Andy, thanks heaps.
    Just to follow exactly what you have done here, you have set Xdata and Ydata as a Range, and somewhere told it to grab 3 columns of data for each.
    For example, if I wished to add another column for both X and Y for an additional data plot for each series, how would I modify the code?
    Thanks again for your help, and sorry if these are all basic 'newbie' type questions.
    Best,
    Matt

  4. #4
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Adding multiple new series to a Scatter chart

    hi Matt, welcome to Excelforum, option, press Run button or run code "test" - ALT+F8, select "test", press Run button
    Attached Files Attached Files

  5. #5
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,461

    Re: Adding multiple new series to a Scatter chart

    You would need to update the 3's in these two lines, which denotes how many columns to use.

        Set YData = NameData.Offset(0, 1).Resize(NameData.Rows.Count, 3)
        Set XData = YData.Offset(0, 3)

  6. #6
    Registered User
    Join Date
    05-28-2012
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Adding multiple new series to a Scatter chart

    Thanks Andy,
    Have I done something wrong here? It does not seem to want to plot when I change the code to denote one column as you suggest above.
    I have also added a title and axis labels to the code, which seems to be working ok.
    Thanks again for your help.
    Cheers,
    Matt

        
    Public Sub CreateChart_SingleCol()
        
        Dim RowIndex As Long
        Dim XData As Range
        Dim YData As Range
        Dim NameData As Range
        Dim Cht As Chart
        
        
        ' reference data
        Set NameData = Range("A3", Range("A3").End(xlDown))
        Set YData = NameData.Offset(0, 1).Resize(NameData.Rows.Count, 1)
        Set XData = YData.Offset(0, 1)
        
        ' get chart and remove existing data
        Set Cht = ActiveSheet.ChartObjects(1).Chart
        With Cht
            Do While .SeriesCollection.Count > 0
                .SeriesCollection(1).Delete
            Loop
        
            ' add rows of data
            For RowIndex = 1 To NameData.Rows.Count
                With .SeriesCollection.NewSeries
                    .Name = "='" & NameData.Parent.Name & "'!" & NameData.Cells(RowIndex, 1).Address(xlR1C1)
                    .Values = "='" & YData.Parent.Name & "'!" & YData.Rows(RowIndex).Address(xlR1C1)
                    .XValues = "='" & XData.Parent.Name & "'!" & XData.Rows(RowIndex).Address(xlR1C1)
                    With .Format.Line
                        .EndArrowheadStyle = msoArrowheadTriangle
                        .ForeColor.RGB = 5296274 ' green
                        .Weight = 1
                    End With
                    .Smooth = True
                End With
            Next
            ' Set Chart Title
            .HasTitle = True
            .ChartTitle.Text = ActiveSheet.Cells(1, 1)
            
            ' Set Axis Title
            .Axes(xlCategory, xlPrimary).HasTitle = True
            .Axes(xlCategory, xlPrimary).AxisTitle.Text = ActiveSheet.Cells(1, 3)
            .Axes(xlValue, xlPrimary).HasTitle = True
            .Axes(xlValue, xlPrimary).AxisTitle.Text = ActiveSheet.Cells(1, 2)
            
        End With
        
    End Sub

  7. #7
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,461

    Re: Adding multiple new series to a Scatter chart

    How would you envisage the line to appear if you only give it 1 data value.

    to see a line you need a minimum of start xy and finish xy

  8. #8
    Registered User
    Join Date
    05-28-2012
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Adding multiple new series to a Scatter chart

    Ah, now I feel foolish. Changing chart type would be useful ;-)
    Thanks very much Andy for your help.
    Regards,
    Matt

  9. #9
    Registered User
    Join Date
    05-28-2012
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Adding multiple new series to a Scatter chart

    Hi Andy,
    Sorry to bother you again. I have been trawling the internet for a VBA solution to format the markers as defined (in the example, determined by column D).
    I have not yet been able to implement any of them successfully.
    Can you help at all?

    Thank you kindly.
    Best,
    Matt
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Scatter Plot Chart with multiple series
    By hyattj in forum Excel General
    Replies: 0
    Last Post: 08-14-2014, 03:36 PM
  2. [SOLVED] Adding multiple series on a single chart
    By Bruza005 in forum Excel Charting & Pivots
    Replies: 4
    Last Post: 10-30-2012, 12:56 AM
  3. XY (Scatter) chart - 1 series/multiple color
    By KHUY in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-20-2012, 06:41 AM
  4. Adding Multiple Series to a chart?
    By Stevie1980 in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 07-10-2008, 06:13 AM
  5. [SOLVED] Adding more series to a XY scatter chart
    By Graham Whitehead in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-05-2006, 11:20 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1