I have a small chart that I am feeding via VBA which is not populating due to an error which I cannot fugure out how to solve.
Below is the code I am using:
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$1"
.FullSeriesCollection(2).Name = "=""Trend Last Year"""
.FullSeriesCollection(2).Values = "=Master!$H$3"
End With
End Sub
Any tip about how to correct and improve the above code?
Thank you
I solved by correcting my code also as per your suggestions:
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$1"
.FullSeriesCollection(1).Name = "=""Trend Current Year"""
.FullSeriesCollection(2).Values = "=Master!$F$3"
.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