VBA创build图表并删除一个系列

我可以为一组数据创build图表。 我不想在图表中包含第179行的数据,但是我需要将它保留在表单中。

如何修改我的图表,通过修改下面的代码,将第178行(标题)和第180:182行作为数据序列?

ActiveSheet.Shapes.AddChart.Select With ActiveChart .SetSourceData Source:=Range("'eod summary'!$B$178:$H$182") .ChartType = xlAreaStacked .Axes(xlCategory).CategoryType = xlCategoryScale End With 

您的范围参考是硬编码的; 如果这总是正确的,那么就build立一个用于SetSourceData的联合引用。

 ActiveSheet.Shapes.AddChart.Select With ActiveChart 'shorthand method .SetSourceData Source:=Range("'eod summary'!$B$178:$H$178,'eod summary'!$B$180:$H$182") 'Union(..., ...) method; more verbose but also more explanatory '.SetSourceData Source:=Union(Range("'eod summary'!$B$178:$H$178"), _ Range("'eod summary'!$B$180:$H$182")) .ChartType = xlAreaStacked .Axes(xlCategory).CategoryType = xlCategoryScale End With 

您可以使用FullSeriesCollection属性来定义标题:

 ActiveSheet.Shapes.AddChart.Select With ActiveChart .SetSourceData Source:=Range("B180:H182") .FullSeriesCollection(1).XValues = "='eod summary'!$B$178:$H$178" .ChartType = xlAreaStacked .Axes(xlCategory).CategoryType = xlCategoryScale End With