2016-12-06 157 views
0

以下是我在工作簿中的每个工作表中创建图表的代码。我在网上发现了这个代码,并对它进行了修改以满足我的需要我是VBA的新手,我不确定如何用“with”语句操作代码。此代码工作良好,直到我更改位于单元格B1中的信息,即我的图表标题。从那以后,我的代码一直在创建2个系列。系列2没有绘制在图表上,而是出现在图例中。当我点击图表来查看它收集的数据时,它不会填充,请参阅imgur链接[1]:https://i.stack.imgur.com/n1jPc.jpg,以便进行可视化。当我查看该系列时,我想保留它显示A3:A630和B3:B630。在excel中隐藏图表VBA

如何删除本系列2?另外,我将文本输入到A1中,并创建了系列3.我想确保系列1在我的图表中可见。

我已经尝试录制宏以删除系列并在我的代码中使用该宏,但由于代码中断,我总是收不到。所录制的宏给 ActiveSheet.ChartObjects(“图1”)。激活 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 

末次

如果上面修改代码的选项2是完全错误的,我也发现以下代码遍历每个工作表上的每个图表并删除系列,但我无法弄清楚如何根据需要修改它。 私人小组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

我从工作中的IT人员那里得到了更多的研究和帮助。

我在with chart语句之前添加了以下代码。 (从珀耳帖乔恩实测值)

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 

结束子