Search code examples
excelexcel-2013excel-chartsvba

Excel Macro Chart creation executes corectly only when stepping through debugger


So I'm having a stress inducing issue with my Excel Macro. When I run the code to create a chart off some table data it breaks saying there is an error creating the sixth series but when I step through the code in the debugger it runs perfectly! Is there something with my approach below or is it another issue I don't know about.

Here is the chart code:

Sub staffing_Chart()
    Dim staffChart
    Set staffChart = ActiveWorkbook.Worksheets("2015   Chart").ChartObjects.Add(Left:=175, Width:=500, Top:=350, Height:=325)
    With staffChart.Chart
        .ChartType = xlAreaStacked
        .HasTitle = True
        .ChartTitle.Text = "All Geo Int Staffing"
        .SetSourceData Source:=Range("B5:G30")
        .FullSeriesCollection(1).Name = "='2015 Chart'!$B$4"
        .FullSeriesCollection(1).XValues = "='2015 Chart'!$A$5:$A$30"
        .FullSeriesCollection(2).Name = "='2015 Chart'!$C$4"
        .FullSeriesCollection(3).Name = "='2015 Chart'!$D$4"
        .FullSeriesCollection(4).Name = "='2015 Chart'!$E$4"
        .FullSeriesCollection(5).Name = "='2015 Chart'!$F$4"
        .FullSeriesCollection(6).Name = "='2015 Chart'!$G$4"
        .Axes(xlCategory).CategoryType = xlCategoryScale
        .Axes(xlCategory).CrossesAt = 1
        .Axes(xlCategory).Crosses = xlAutomatic
        .Axes(xlCategory).TickMarkSpacing = 5
        .Axes(xlCategory).TickLabelSpacing = 5
        .Axes(xlCategory).TickLabels.NumberFormat = "m/d/yyyy"
        .Axes(xlCategory).TickLabels.NumberFormat = "[$-409]mmm-yy;@"
        .Axes(xlCategory).TickLabels.NumberFormat = "[$-409]mmm;@"
    End With
End Sub

And here is the table:

enter image description here

EDIT

In answer to your suggestion Scott using your code I get this chart when I run in normally, enter image description here

But if I step through your code in the debugger it does work correctly so that is a good suggestion from a optimization front.

Nonetheless my problem persists.


Solution

  • Why not just rely on Excel's built-in intelligence when building this chart and set the Source Range to include the Column Labels and x-Axis Category Labels.

    The below code worked flawlessly for me, either in full execution or stepping through:

    Sub staffing_Chart()
    
        Dim staffChart
        Set staffChart = ActiveWorkbook.Worksheets("2015 Chart").ChartObjects.Add(Left:=175, Width:=500, Top:=350, Height:=325)
    
        With staffChart.Chart
    
            .ChartType = xlAreaStacked
            .HasTitle = True
            .ChartTitle.Text = "All Geo Int Staffing"
            .SetSourceData Source:=Range("A4:G30")
    
            With .Axes(xlCategory)
                .CategoryType = xlCategoryScale
                .CrossesAt = 1
                .Crosses = xlAutomatic
                .TickMarkSpacing = 5
                .TickLabelSpacing = 5
                .TickLabels.NumberFormat = "[$-409]mmm;@"
            End With
    
        End With
    
    End Sub