我有一个宏,可在Excel图表中隐藏该行。但是,问题是,即使宏从图表中删除了该线,它仍在图例中显示为空白。
如何修改我的代码以便解决此问题?
代码:
Sub Macro1()
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.FullSeriesCollection(2).Format.Line.Visible = _
Not ActiveChart.FullSeriesCollection(2).Format.Line.Visible
End Sub
答案 0 :(得分:1)
添加以下行以从图例中删除2ns系列:
ActiveChart.Legend.LegendEntries(2).Delete
更干净的方法是使用完全定义的对象而不使用ActiveSheet
或ActiveChart
:
Option Explicit
Sub Macro1()
Dim MyCht As ChartObject
' set the chart object
Set MyCht = Worksheets("Sheet1").ChartObjects(1) ' "Chart 1") ' modify the "Sheet1" and "Chart 1" according to your needs
With MyCht
.Chart.FullSeriesCollection(2).Format.Line.Visible = Not .Chart.FullSeriesCollection(2).Format.Line.Visible
.Chart.Legend.LegendEntries(2).Delete
End With
End Sub
答案 1 :(得分:0)
您可以使用IsFiltered
:
Sub tester()
Dim cht As Chart
Set cht = ActiveSheet.ChartObjects(1).Chart
HideShowSeries cht, "Series1", False 'hide series1
HideShowSeries cht, "Series1", True 'show series1
HideShowSeries cht, "", False 'hide all series
HideShowSeries cht, "", True 'show all series
End Sub
Sub HideShowSeries(cht As Chart, seriesname As String, show As Boolean)
Dim s As Series
For Each s In cht.FullSeriesCollection
If s.Name = seriesname Or seriesname = "" Then
s.IsFiltered = Not show
End If
Next s
End Sub