Search code examples
excelvbaexcel-charts

Scatter and Scatter lines dont match obtained results


I have the following code

Dim sh As Worksheet
Set sh = ActiveWorkbook.Worksheets("Graphs")
Dim chrt As Chart
Set chrt = sh.Shapes.AddChart.Chart
Set s1 = chrt.SeriesCollection.NewSeries
Set s2 = chrt.SeriesCollection.NewSeries
Set s3 = chrt.SeriesCollection.NewSeries
Set s4 = chrt.SeriesCollection.NewSeries
Set s5 = chrt.SeriesCollection.NewSeries

With s1
    .ChartType = xlXYScatterLines
    'Change to what your series should be called
    .Name = "ConcreteCS"
    .XValues = "=Graphs!$E$48:$E$52"
    .Values = "=Graphs!$F$48:$F$52"
End With

With s2
    .ChartType = xlXYScatter
    .Name = "Asl_base_corner"
    .XValues = "=Graphs!$K$48:$K$49"
    .Values = "=Graphs!$L$48:$L$49"
End With

With s3
    .ChartType = xlXYScatter
    .Name = "Asl_base_mid"
    .XValues = "=Graphs!$K$52:$k$100"
    .Values = "=Graphs!$L$52:$L$100"
End With

With s4
    .ChartType = xlXYScatter
    .Name = "Asl_add_corner"
    .XValues = "=Graphs!$Q$48:$Q$49"
    .Values = "=Graphs!$R$48:$R$49"
End With

With s5
    .ChartType = xlXYScatter
    .Name = "Asl_add_mid"
    .XValues = "=Graphs!$Q$52:$Q$100"
    .Values = "=Graphs!$R$52:$R$100"
End With

With chrt
    .ChartArea.Left = 100
    .ChartArea.Top = 750
    .ChartArea.Height = 200
    .ChartArea.Width = 200
    .Axes(xlValue).MajorGridlines.Delete
    .HasAxis(xlCategory, xlPrimary) = False
    .HasAxis(xlCategory, xlSecondary) = False
    .HasAxis(xlValue, xlPrimary) = False
    .HasAxis(xlValue, xlSecondary) = False
    .Axes(xlCategory).MinimumScale = -b / 2
    .Axes(xlCategory).MaximumScale = b + b / 2
    .Axes(xlValue).MinimumScale = -h / 2
    .Axes(xlValue).MaximumScale = h + h / 2

    For ii = 1 To chrt.Legend.LegendEntries.Count Step 1
        .Legend.LegendEntries(ii).Delete
    Next

End With

But the result is scatter with lines for s1, s2 and s3 and scatter for s4 and s5. The strange thing is that I've only defined scatter with lines for s1. Also, when I try to delete the legends for all series, from index 4 to 5 gives me error, although the index exists (chrt.Legend.LegendEntries.Count = 5).

Please help me!!! :)

Many thanks


Solution

  • I think you should delete Legend in that way :

    For ii = chrt.Legend.LegendEntries.Count To 1 Step -1
            .Legend.LegendEntries(ii).Delete
        Next
    

    But the result is scatter with lines for s1, s2 and s3 and scatter for s4 and >s5.

    You are sure that point from series s2, s3 are not on the same line that s1? and this only look like line? Or point are big and looks like connected?

    On my computer your code do it properly. It is strange enter image description here