I am trying to create a chart that plots X and Y points, but draws a line between each X point for each series. Sort of like this:
However my issue is how I define my Xvalues and Values in the SeriesCollection of VBA. This is because my values are actually stored in arrays. I have used arrays because I have data that is:
ID Age1 Age2 Per
1 21 22 54.2%
2 19 23 68.6%
3 18 23 42.0%
4 30 33 45.1%
5 17 19 33.0%
6 19 22 41.3%
7 22 27 20.4%
8 19 20 56.4%
9 30 33 42.8%
10 21 22 59.7%
So I need to plot on the X-axis the value in Age1 and Age2. The corresponding Y value is in Per column. But I need to create a new series for each ID. I have the following code and tagged where the issue is. Note it does not compile. I think the issue is how I pass through the values to create the XY chart. I only want to pass through the values onto the chart.
Sub name()
Dim age1 As Variant
Dim age2 As Variant
Dim per1 As Variant
Dim per2 As Variant
Dim id As Variant
Dim ln As Integer
Dim cht As Chart
Dim wb As Workbook
Dim ws As Worksheet
Dim xdata As Varaint
Dim ydata As Varaint
Set wb = ThisWorkbook
Set ws = wb.Sheets(1)
id = ws.Range("A2", Range("A2").End(xlDown)).Value
age1 = ws.Range("B2", Range("B2").End(xlDown)).Value
age2 = ws.Range("C2", Range("C2").End(xlDown)).Value
per1 = ws.Range("D2", Range("D2").End(xlDown)).Value
per2 = ws.Range("D2", Range("D2").End(xlDown)).Value
ln = UBound(id) - LBound(id) + 1
Set cht = ws.ChartObjects("chart").Chart
With cht
.ChartArea.ClearContents
.ChartType = xlXYScatter
For i = 1 To ln
xdata=Array(age1(i),age2(i)) 'I assumed this would get the values in age1(i) and age2(i) and plot those as the xdata
ydata=Array(per1(i),per2(i)) 'Same error as above, but would use ydata
.SeriesCollection.NewSeries
.SeriesCollection(i).XValues = xdata
.SeriesCollection(i).Values = ydata
.SeriesCollection(i).Name = id(i)
Next i
End With
End Sub

xdataandydataas Range objects, but you've never instantiated them as ranges. There may be further problems with the actual assignment statementxdata = ...but you can't put a value in aNothing, and that's what an un-instantiated range object is :)xdataandydataas Variants. The compile error is Syntax error. On the line with the comments(age1(i),age2(i))is not valid notation of anything in VBA. I think this is what you meant:xdata = Array(age1(i), age2(i))but without testing it I'm pretty sure that won't work, because it looks like you're trying to put an array of bothagedata into a single seriesXValues. Each series must have its own XValues.