【问题标题】:Hiding Series in Chart in excel VBA在excel VBA中的图表中隐藏系列
【发布时间】:2016-12-06 16:39:03
【问题描述】:

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

在我更改单元格 B1 中的信息(我的图表标题)之前,这段代码运行良好。从那时起,我的代码一直在创建 2 个系列。系列 2 未绘制在图表上,但出现在图例中。当我单击图表以查看它正在收集的数据时,它没有填充,正如这张图片所展示的那样

当我查看我想保留的系列时,它确实显示 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 之外的所有系列。

【问题讨论】:

    标签: excel vba charts


    【解决方案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
    

    结束子

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多