Currently, my code only plots chart for alternate column of y "mean values" in the region from Column A to E with same x values(represented as work week). But now if I want to include the data from another region such as the entire alternate column of y "ideal mean values" which also has the same x values as highlighted in figure 1 onto the same chart, how do i include this data too for plotting in VBA?
figure 1
Currently
Expected
Expected output
current code
Sub plotgraphs()
Call meangraph
End Sub
Private Sub meangraph()
Dim i As Long, c As Long
Dim shp As Shape
Dim Cht As chart, co As Shape
Dim rngDB As Range, rngX As Range, rngY As Range,yourOtherRange As Range, rngdb1 As Range
Dim Srs As Series
Dim ws As Worksheet
Set ws = Sheets("Data")
Set rngDB = ws.Range("A1").CurrentRegion
Set rngX = rngDB.Columns(1)
Set rngY = rngDB.Columns(2)
Do While Application.CountA(rngY) > 0
Set co = Worksheets("meangraphs").Shapes.AddChart
Set Cht = co.chart
With Cht
.ChartType = xlXYScatter
'remove any data which might have been
' picked up when adding the chart
Do While .SeriesCollection.Count > 0
.SeriesCollection(1).Delete
Loop
'add the data
With .SeriesCollection.NewSeries()
.XValues = rngX.Value
.Values = rngY.Value
End With
'formatting...
With Cht.Axes(xlValue)
.MinimumScale = 5
.MaximumScale = 20
.TickLabels.NumberFormat = "0.00E+00"
End With
Cht.Axes(xlCategory, xlPrimary).HasTitle = True
Cht.Axes(xlValue, xlPrimary).HasTitle = True
End With
Set rngY = rngY.Offset(0, 2) 'next y values
With Cht
.ChartType = xlXYScatter
'remove any data which might have been
' picked up when adding the chart
Do While .SeriesCollection.Count > 0
.SeriesCollection(1).Delete
Loop
'add the first series
With .SeriesCollection.NewSeries()
.XValues = rngX.Value
.Values = yourOtherRange.Value
End With
'second series
With .SeriesCollection.NewSeries()
.XValues = rngX.Value
.Values = yourOtherRange.Offset(0, 6).Value
End With
end with
Loop
end sub





With .SeriesData.NewSeries()part: all you need to do is swap outrngY.ValueforyourOtherRange.Value