在excel VBA中隐藏图表中的系列

时间:2016-12-06 16:39:03

标签: excel vba charts

以下是我在工作簿中的每个工作表中创建图表的代码。我在网上找到了这个代码并根据我的需要进行了修改。我是VBA的新手,不知道如何操作包含With语句的代码。

此代码运行良好,直到我更改位于单元格B1(我的图表标题)中的信息。从那以后,我的代码一直在创建2个系列。系列2未在图表上绘制,但显示在图例中。当我点击图表查看它正在收集的数据时,它不会填充,因为此图像显示

enter image description here

当我查看我要保留的系列时,它确实显示A3:A630和B3:B630。

如何删除此系列2?

相关:我还在A1中输入了文本并创建了系列3.我想确保在我的图表上只能看到系列1。

我已经尝试录制宏来删除系列并在我的代码中使用它,但我总是收到

  

由于代码中断而无法继续

录制的宏产生了:

ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.FullSeriesCollection(2).Delete

我还找到了一种隐藏系列的方法,但是当我在.SeriesCollection阻止之后插入它时,它会再次出现“中断代码”错误。

Selection.Format.Line.Visible = msoFalse 

创建图表的原始代码

  Sub chartcreation()
    Dim sh As Worksheet
    Dim chrt As Chart

    For Each sh In ActiveWorkbook.Worksheets
        Set chrt = sh.Shapes.AddChart.Chart

        With chrt
            'Data?
            .ChartType = xlXYScatterSmooth
            .SeriesCollection.NewSeries
            .SeriesCollection(1).Name = sh.Range("B1").Value
            .SeriesCollection(1).XValues = sh.Range("$A$3:$A$630")
            .SeriesCollection(1).Values = sh.Range("$B$3:$B$630")

            'Titles
            .HasTitle = True
            .ChartTitle.Text = sh.Range("B1").Value
            .Axes(xlCategory, xlPrimary).HasTitle = True
            .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text =     sh.Range("A2")
            .Axes(xlValue, xlPrimary).HasTitle = True
            .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = sh.Range("B2")

            'Formatting
            .Axes(xlCategory).HasMinorGridlines = False
            .Axes(xlValue).HasMajorGridlines = True
            .Axes(xlCategory).MinimumScale = 15
            .Axes(xlCategory).MaximumScale = 90
            .Axes(xlValue).HasMinorGridlines = False
            .Axes(xlValue).MinimumScale = 0
            .Axes(xlValue).MaximumScale = 60
            .HasLegend = True
        End With
    Next
End Sub

如果以上2个修改代码的选项完全错误,我确实找到了以下代码,这些代码遍历每个工作表上的每个图表并删除了系列,但我无法弄清楚如何根据我的需要修改它。

Private Sub Workbook_Open()

  Dim Sht As Worksheet
  Dim ShtName As String
  Dim R As Range
  Dim ASht As Worksheet

  Set R = ActiveCell        'Save the activecell
  Set ASht = ActiveSheet    'Save the activesheet

  Application.ScreenUpdating = False

  For Each Sht In ActiveWorkbook.Sheets
    ShtName = Sht.Name
    Select Case ShtName
      Case "One", "Two", "Three"            'Charts are on multiple sheets
        Call DeleteLegendEntries(Sht)
    End Select
  Next Sht

  ASht.Activate                              'Back to original sheet
  R.Activate                                 'Back to original cell
  Application.ScreenUpdating = True

End Sub

重申:我想在一个工作簿的每个工作表中隐藏或删除除已重命名的系列1之外的所有系列。

1 个答案:

答案 0 :(得分:1)

我通过更多的研究和IT人员的帮助来解决这个问题。

我在with chart语句之前添加了以下代码。 (从Jon Peltier找到)

Do Until chrt.SeriesCollection.Count = 0
chrt.SeriesCollection(1).Delete
Loop

整个代码看起来像

Sub chartcreation()
Dim sh As Worksheet
Dim chrt As Chart


For Each sh In ActiveWorkbook.Worksheets
    Set chrt = sh.Shapes.AddChart.Chart

Do Until chrt.SeriesCollection.Count = 0
chrt.SeriesCollection(1).Delete
Loop

    With chrt
        'Data?
        .ChartType = xlXYScatterSmooth
        .SeriesCollection.NewSeries
        .SeriesCollection(1).Name = sh.Range("B1").Value
        .SeriesCollection(1).XValues = sh.Range("$A$3:$A$630")
        .SeriesCollection(1).Values = sh.Range("$B$3:$B$630")

        'Titles
        .HasTitle = True
        .ChartTitle.Text = sh.Range("B1").Value
        .Axes(xlCategory, xlPrimary).HasTitle = True
        .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = sh.Range("A2")
        .Axes(xlValue, xlPrimary).HasTitle = True
        .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = sh.Range("B2")

        'Formatting
        .Axes(xlCategory).HasMinorGridlines = False
        .Axes(xlValue).HasMajorGridlines = True
        .Axes(xlCategory).MinimumScale = 15
        .Axes(xlCategory).MaximumScale = 90
        .Axes(xlValue).HasMinorGridlines = False
        .Axes(xlValue).MinimumScale = 0
        .Axes(xlValue).MaximumScale = 60
        .HasLegend = True

    End With
Next

End Sub

相关问题