Hi All,
I am trying to plot a graph.
I have one workbook with two worksheets, they are Sheet1 and Sheet2.
There are lots of rows of data in both Sheets, I am plotting graphs from the data in Sheet1 and placing them on sheet1 near the data that I am using. Then for one of the graphs I am accessing data from Sheet2, but placing this graph on Sheet1 to keep all of the graphs together for display. Here’s where I have the problem, when I was testing the code on a certain Workbook, it all worked fine, but when I opened up another Workbook, with only two worksheets – Sheet1 and Sheet2, it appears to be exactly the same as my tests Workbook it come up with the “Run-time error 9” – Subscript out of range.
And stops at this line
Set wsData = ThisWorkbook.Sheets("Sheet2") 'sheet2 / Residual
Here is the is cut down code
Sub Residual()
Dim wsData As Worksheet
Dim wsChart As Worksheet
Dim chtChart As chartObject
Dim lastRow As Long
' Set the data and chart worksheets
Set wsData = ThisWorkbook.Sheets("Sheet2") 'sheet2 / Residual
Set wsChart = ThisWorkbook.Sheets("Sheet1") 'sheet1 / Fatigue
' Find the last row with data in column C
lastRow = wsData.Cells(wsData.Rows.Count, "C").End(xlUp).Row
' Create a new chart object on the chart worksheet
Set chtChart = ActiveSheet.ChartObjects.Add(Left:=100, Width:=375, Top:=75, Height:=225)
' Configure chart properties
With chtChart.Chart
.ChartType = xlXYScatterLines
' Set data source directly
With .SeriesCollection.NewSeries
.XValues = wsData.Range("d9:d" & lastRow) ' X-axis (Displacement)
.Values = wsData.Range("c9:c" & lastRow) ' Y-axis (Force)
End With
End With
End Sub
Hopefully someone can see what I cannot or offer some advice.
Thanks
Nat
Bookmarks