I am trying to code for a Scatter Plot using smooth lines with VBA. I am trying to take data off of a worksheet and create a scatter plot with lines and no markers in the same workbook different sheet using VBA.
This is part snapshot of my worksheet
The values below 247 and between 263 to 455 in column A will have corresponding -1.75 in column B.
The x values are in range A1:A401
y-values are in range B1:B401
Also I want to have title to my graph and X and Y axis labelled. I am having trouble figuring how to get the y-values to plot with the x-values instead of excel making two seperate lines on the chart.
This is graph I need
This is the code I have used
Set xData = ThisWorkbook.Worksheets(2).Range("A1:A" & LastRow_this)
Set yData = ThisWorkbook.Worksheets(2).Range("B1:B" & LastRow_this)
Set GraphRange = Union(xData, yData)
'Create a chart
Set cht = ThisWorkbook.Worksheets(1).Shapes.AddChart2
'Give chart some data
cht.Chart.SetSourceData Source:=GraphRange
'Determine the chart type
cht.Chart.ChartType = xlXYScatterLines
This is what it gives me in Excel.
How can I get the desired result ?
Also what can I do if the range is dynamic ?
Set cht = ThisWorkbook.Worksheets(1).Shapes.AddChart2(, xlXYScatterChartLines)
so the chart starts out as the type you want. – Jon Peltier