+ Reply to Thread
Results 1 to 6 of 6

Adding and removing series to a chart

Hybrid View

  1. #1
    Registered User
    Join Date
    03-13-2014
    Location
    Miami
    MS-Off Ver
    Excel 2010
    Posts
    42

    Adding and removing series to a chart

    Hi Everyone,

    I have a problem that should be simple to solve, but its giving me a headache.

    I want to use 4 toggle buttons to be able to add and remove 4 different series to a chart. When the toggle button is clicked, a specific series is added to a chart, and when it is unclicked then the series would be removed.

    The problem I am running into is that unless I add and remove each series in a specific order, I will get an error because I may be trying to remove a series whose series number is not on the series collection in the chart.

    for example: if I remove series number 2 with the toggle button for that specific series, then the toggle button for series number 3 will remove the old series 4 (which is now 3), and the toggle button for series 4 won't work because series 4 will now have become series 3.

    Therefore, would there be a way to remove a series based on the name of the series, rather than the series collection number?

    Any help is much appreciated.

    Thanks!

  2. #2
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: Adding and removing series to a chart

    Yes. In general, you can refer to anything in excel vba either by collection(#) or collection("name"). So if you put the name of the series in quotes where you have been putting the number, it should work.
    1. Include a sample workbook with an example of the result you want
    2. Use [CODE] and [FORMULA] wrappers for your VBA code or excel formulas
    3. If your question has been answered, mark the thread as SOLVED and click on the "Add Rep" star to thank whoever helped you.

  3. #3
    Forum Expert
    Join Date
    06-18-2004
    Location
    Canada
    MS-Off Ver
    Office 2016
    Posts
    1,474

    Re: Adding and removing series to a chart

    Try to adapt the following...

    Worksheets("Sheet1").ChartObjects("Chart 1").Chart.SeriesCollection("SeriesName").Delete
    
    With Worksheets("Sheet1").ChartObjects("Chart 1").Chart.SeriesCollection.NewSeries
        .XValues = Worksheets("Sheet1").Range("A2:A5").Value
        .Values = Worksheets("Sheet1").Range("B2:B5").Value
        .Name = "SeriesName"
        .PlotOrder = 1
    End With
    Hope this helps!

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

    Re: Adding and removing series to a chart

    Why not hide the data rather than delete/insert series. If plot visible cells is not enabled the chart will delete/insert the series for you.
    Cheers
    Andy
    www.andypope.info

  5. #5
    Registered User
    Join Date
    03-13-2014
    Location
    Miami
    MS-Off Ver
    Excel 2010
    Posts
    42

    Re: Adding and removing series to a chart

    Hi Andy,

    I've done something similar to what you're suggesting by hiding and showing columns, in another project, but the problem I ran into is that I had too much data to display on the screen (I had 6 charts, and over 40 columns with with several 1000 rows of data), and excel would become really slowly because it had to hide and show much data.

    Domenic's answer up above almost works perfectly, except for the plot order. if remove series 3 and 4, and later want to only bring back series 4, then there will be an error. is there a way to code so that if there is an error in the plot order, then make it the last series in the plot?

    thanks.

  6. #6
    Forum Expert
    Join Date
    06-18-2004
    Location
    Canada
    MS-Off Ver
    Office 2016
    Posts
    1,474

    Re: Adding and removing series to a chart

    Try something like this...

    Dim MyChart         As Chart
    Dim MySeries        As Series
    Dim MyPlotOrder     As Integer
    
    Set MyChart = Worksheets("Sheet1").ChartObjects("Chart 1").Chart
    
    MyPlotOrder = 4
    If MyPlotOrder > MyChart.SeriesCollection.Count + 1 Then
        MyPlotOrder = MyChart.SeriesCollection.Count + 1
    End If
    
    Set MySeries = MyChart.SeriesCollection.NewSeries
    With MySeries
        .XValues = Worksheets("Sheet1").Range("A2:A5")
        .Values = Worksheets("Sheet1").Range("E2:E5")
        .Name = Worksheets("Sheet1").Range("E1")
        .PlotOrder = MyPlotOrder
    End With
    Hope this helps!

+ 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. [SOLVED] Clustered Column Chart - Removing Gaps for Zero Value Series
    By rzrbkpk in forum Excel Charting & Pivots
    Replies: 7
    Last Post: 08-30-2013, 11:02 AM
  2. Adding two series' to a chart
    By RowanB in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-30-2012, 06:54 AM
  3. Adding Series to a chart
    By ih8xc in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-29-2010, 03:19 PM
  4. Adding series to a xy plot chart in vba
    By OIN in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 02-25-2008, 07:51 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

Tags for this Thread

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