简体   繁体   English

使用VBA从图表中删除系列

[英]Remove series from chart with VBA

I am creating a chart in excel via vba code. 我正在通过VBA代码在excel中创建图表。 I am using contiguous data and the chart pops up no problem, however there is an extra series named "Series 3" that I didn't ask for and need to get rid of (via deleting it or omitting in the first place). 我使用的是连续数据,图表弹出没有问题,但是我并没有要求过要删除的额外系列“ Series 3”(首先删除它或将其省略)。 It has no data in it but needs to be removed from the legend at least. 它没有数据,但至少需要从图例中删除。 Here is my code : 这是我的代码:

Dim MyChtObj As ChartObject
Dim Sht1 As Worksheet
Dim ShtName As String

Set Sht1 = Worksheets("Parameter Forecasts")
ShtName = Sht1.Name
Set MyChtObj = Sht1.ChartObjects.Add(100, 100, 500, 500)

Set a = Sht1.Range("E37", Sht1.Range("E37").End(xlToRight))
Set b = Sht1.Range("E38", Sht1.Range("E38").End(xlToRight))
Set InputData = Union(a, b)

With MyChtObj.Chart
    .ChartType = xlLineMarkers
    .SetSourceData InputData
    .PlotBy = xlRows
    .SeriesCollection.NewSeries.XValues = Sht1.Range("F36", Sht1.Range("F36").End(xlToRight))
End With

I have already tried: 我已经尝试过:

MyChtObj.SeriesCollection(3).Delete

But this does not work. 但这是行不通的。

Thanks in advance, Max 预先感谢,马克斯

The SeriesCollection is part of the ChartObject.Chart object, and not the ChartObject . SeriesCollectionChartObject.Chart对象的一部分,而不是ChartObject

Therfore, replace your line of: 因此,请替换以下行:

MyChtObj.SeriesCollection(3).Delete

With: 带有:

MyChtObj.Chart.SeriesCollection(3).Delete

Your line of code 您的代码行

.SeriesCollection.NewSeries.XValues = Sht1.Range("F36", Sht1.Range("F36").End(xlToRight))

is adding a third series via .NewSeries . 正在通过.NewSeries添加第三个系列。 Change it to 更改为

.SeriesCollection(1).XValues = Sht1.Range("F36", Sht1.Range("F36").End(xlToRight))

Now there's nothing to delete. 现在没有任何要删除的内容。

THis doesn't seem to work always 这似乎并不总是有效

.SeriesCollection(1).XValues = Sht1.Range("F36", Sht1.Range("F36").End(xlToRight))

This wont work if there is no series 3 to delete (some times randomly this series 3 doesn't appear by default then you'll get an error for this line ) 如果没有要删除的系列3,则此方法将无效(有时默认情况下该系列3不会随机出现,则此行会出现错误)

MyChtObj.Chart.SeriesCollection(3).Delete

Here's a fast n dirty trick. 这是一个快速的肮脏技巧。 Add this (repeated 3 times here but repeat more depending on your sheet because different sheets depending on the nr of cells populated, generate different numbers of random series). 添加此内容(此处重复3次,但根据您的工作表重复更多,因为不同的工作表取决于填充的单元格的nr,会生成不同数量的随机序列)。 It will keep deleting the series that falls into the 3rd position in the "list" with every deletion until nr or series is 2 每次删除时,它将不断删除落入“列表”中第三位的序列,直到nr或序列为2

        If .SeriesCollection.Count > 2 Then
MyChtObj.Chart.SeriesCollection(3).Delete
        End If

        If .SeriesCollection.Count > 2 Then
MyChtObj.Chart.SeriesCollection(3).Delete
        End If

        If .SeriesCollection.Count > 2 Then
MyChtObj.Chart.SeriesCollection(3).Delete
        End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM