View Full Version : [SOLVED:] Problem deleting a legendEntry
Mister_joe
03-30-2014, 11:26 AM
Hello All,
Please, I am trying to delete a specific LegendEntry using ActiveChart.Legend.LegendEntries(1).Delete
The problem?  The command simply deletes the entire legend.  What am I missing?  I am using Office 2007.
Please, help in anyway you can.  Thanks.
Help us helping you anyway you can by posting a sample workbook.
Mister_joe
03-30-2014, 12:29 PM
Here's the section of the code that is giving me problems:
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatterSmoothNoMarkers
ActiveChart.HasAxis(xlValue, xlPrimary) = True
ActiveChart.HasAxis(xlCategory, xlPrimary) = True
ActiveSheet.ChartObjects(ActiveSheet.ChartObjects.Count).Activate   
        With ActiveChart
            .HasTitle = True
            .ChartTitle.Text = "Some texts"
            .ChartTitle.Select
            With Selection
                .Orientation = xlHorizontal
                .HorizontalAlignment = xlCenter
                .VerticalAlignment = xlBottom
                .IncludeInLayout = False
            End With
        
        Dim i As Integer
        .HasLegend = True
        .Legend.Left = ActiveChart.ChartArea.Left + 10
        .Legend.Top = ActiveChart.ChartArea.Top + 50
        .Legend.Select
        With Selection
            .Font.Name = "Verdana"
            .Font.Size = 10
            .Font.Bold = True
        End With
            ActiveChart.Legend.LegendEntries(1).Delete
        .Refresh
    End With
Dim rng As Variant
             ActiveWorkbook.Worksheets("XYZ").Activate   
            ActiveSheet.ChartObjects(ActiveSheet.ChartObjects.Count).Activate    
            With ActiveChart
            For Each rng In ChartDataSource
                    .SeriesCollection.NewSeries  'this creates a series object and adds it to the SerriesCollection.
                    .SeriesCollection(ActiveChart.SeriesCollection.Count).Name = rng.Name.Name
                    .SeriesCollection(ActiveChart.SeriesCollection.Count).XValues = Range(rng.Cells(2, XcolIndex), rng.Cells(rng.Rows.Count, XcolIndex))  'x-axis.  
                    .SeriesCollection(ActiveChart.SeriesCollection.Count).Values = Range(rng.Cells(2, YcolIndex), rng.Cells(rng.Rows.Count, YcolIndex))   'y-axis
                    
                    Randomize
                    red = Int(Rnd() * 255)
                    Randomize
                    blue = Int(Rnd() * 255)
                    Randomize
                    green = Int(Rnd() * 255)
                    
                    .SeriesCollection(ActiveChart.SeriesCollection.Count).Select
                        With Selection.Format.Line
                                .Style = msoLineSingle
                                .Weight = 4
                                .Visible = msoTrue
                                .Transparency = 0
                                .ForeColor.RGB = RGB(red, green, blue)
                                .DashStyle = msoLineSolid
                        End With
         Next 'rng
You probably would like to know why I want to delete the first LegendEntry.  It is because the header row of my data is listed as the first legend entry.  When I step through the code, the header row is not included in the legend, but when I run the code normally, the header row is included as the first entry in the legend.  I am unable to understand why this is happening.  So, my work around was to try and delete the unwanted entry.
You must have another dictionary than I have; see the lemma 'workbook'.
Mister_joe
03-31-2014, 12:55 AM
Please, explain the information you require from me a little more.  The workbook has more than two thousand lines of codes and it contains controlled data that I cannot attach here.  Except for the legend entry, the macro is working according to the original intention. I will try to duplicate the problem with another set of data and post here.
Mister_joe
03-31-2014, 03:32 AM
Thanks all.  I think I found a work around to my challenge.  The command ActiveChart.Legend.LegendEntries(1).LegendKey.Delete resolved it.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.