I am using the following code to place a tite on a chart:-
 With Sheets("SalesChartData")
    
    'ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
     ActiveSheet.Shapes.AddChart2(227, xlLineMarkers).Select
    End With
     With ActiveSheet
               
                   ActiveChart.HasTitle = True
                       ActiveChart.SetSourceData Source:=Range("SalesChartData!C3:O12")
                             ActiveChart.SetElement (msoElementChartTitleAboveChart)
                                ActiveChart.Parent.Name = "MyChart"
                                    ActiveChart.ChartTitle.Text = "MONTHS SALES  " & Sheets("GrossMargin").Range("G5").Value 'xxxxxxxxxxx
                                       ' ActiveChart.SetElement (msoElementPrimaryCategoryAxisShow)
                                          'ActiveChart.SetElement (msoElementPrimaryValueAxisShow)
                                        ActiveChart.SetElement (msoElementLegendBottom)
                                    Sheets("SalesChartData").Shapes("MyChart").ScaleWidth 2, msoFalse, _
                                msoScaleFromBottomRight
                            
                        Sheets("SalesChartData").Shapes("MyChart").ScaleHeight 1.25, msoFalse, _
                  msoScaleFromTopLeft
     End With
When I run this code I get the error "This chart does not have a title". I do not get this message every time I run the code, although I have not yet been able to characterise the situations where it works.
I am new to chart coding so I am using Macros to help me. I must have gone wrong somewhere however and need some help to show me where.
John