无法找出错误所在:对象图表的 SetElement 失败
Cannot figure out where the error is: SetElement of Object Chart failed
我通过 VBA 提供了一个小图表,由于错误我无法确定如何解决,因此没有填充。
下面是我使用的代码:
Sub MyYOYTrendChart()
Dim DestinationWs As Worksheet
Dim chartsWs As Worksheet
Set chartsWs = ThisWorkbook.Worksheets("Charts")
Set DestinationWs = ThisWorkbook.Worksheets("Master")
With chartsWs.ChartObjects("MYCHART").Chart
.HasTitle = True
.ChartTitle.Text = "My YOY Trends"
.SetElement (msoElementDataLabelOutSideEnd)
.SetElement (msoElementLegendRight)
.SetElement (msoElementChartTitleAboveChart)
.FullSeriesCollection(1).Name = "=""Trend Current Year"""
.FullSeriesCollection(1).Values = "=Master!$H"
.FullSeriesCollection(2).Name = "=""Trend Last Year"""
.FullSeriesCollection(2).Values = "=Master!$H"
End With
End Sub
关于如何更正和改进上述代码的任何提示?
谢谢
我也根据您的建议通过更正我的代码解决了问题:
Sub MyYOYTrendChart()
Dim DestinationWs As Worksheet
Dim chartsWs As Worksheet
Set DestinationWs = ThisWorkbook.Worksheets("Master")
Set chartsWs = ThisWorkbook.Worksheets("Charts")
Dim cht As Chart
Set cht = chartsWs.ChartObjects("MYCHART").Chart
With cht
.ChartType = xlColumnClustered
.HasTitle = True
.ChartTitle.Text = "My YOY Trends"
.SeriesCollection.NewSeries
.FullSeriesCollection(1).Values = "=Master!$F"
.FullSeriesCollection(1).Name = "=""Trend Current Year"""
.FullSeriesCollection(2).Values = "=Master!$F"
.FullSeriesCollection(2).Name = "=""Trend Last Year"""
If .SeriesCollection.Count > 2 Then
.SeriesCollection(3).Delete
End If
.SetElement msoElementDataLabelOutSideEnd
.SetElement msoElementLegendRight
.SetElement msoElementChartTitleAboveChart
End With
End Sub
我通过 VBA 提供了一个小图表,由于错误我无法确定如何解决,因此没有填充。
下面是我使用的代码:
Sub MyYOYTrendChart()
Dim DestinationWs As Worksheet
Dim chartsWs As Worksheet
Set chartsWs = ThisWorkbook.Worksheets("Charts")
Set DestinationWs = ThisWorkbook.Worksheets("Master")
With chartsWs.ChartObjects("MYCHART").Chart
.HasTitle = True
.ChartTitle.Text = "My YOY Trends"
.SetElement (msoElementDataLabelOutSideEnd)
.SetElement (msoElementLegendRight)
.SetElement (msoElementChartTitleAboveChart)
.FullSeriesCollection(1).Name = "=""Trend Current Year"""
.FullSeriesCollection(1).Values = "=Master!$H"
.FullSeriesCollection(2).Name = "=""Trend Last Year"""
.FullSeriesCollection(2).Values = "=Master!$H"
End With
End Sub
关于如何更正和改进上述代码的任何提示?
谢谢
我也根据您的建议通过更正我的代码解决了问题:
Sub MyYOYTrendChart()
Dim DestinationWs As Worksheet
Dim chartsWs As Worksheet
Set DestinationWs = ThisWorkbook.Worksheets("Master")
Set chartsWs = ThisWorkbook.Worksheets("Charts")
Dim cht As Chart
Set cht = chartsWs.ChartObjects("MYCHART").Chart
With cht
.ChartType = xlColumnClustered
.HasTitle = True
.ChartTitle.Text = "My YOY Trends"
.SeriesCollection.NewSeries
.FullSeriesCollection(1).Values = "=Master!$F"
.FullSeriesCollection(1).Name = "=""Trend Current Year"""
.FullSeriesCollection(2).Values = "=Master!$F"
.FullSeriesCollection(2).Name = "=""Trend Last Year"""
If .SeriesCollection.Count > 2 Then
.SeriesCollection(3).Delete
End If
.SetElement msoElementDataLabelOutSideEnd
.SetElement msoElementLegendRight
.SetElement msoElementChartTitleAboveChart
End With
End Sub