I am so confused on why setting the xvalues in the fashion I am doing below would make it error?
I checked the debugger and the variables are correct,i.e. the sheetname is referring to the correct name, the column and row numbers are correct.

Anyone know a better or error-free way of setting the Xvalues? I looked everywhere on the internet, and it seems returning a range by using the Range property to Values property is the most efficient way.

This would error if I replot different data to the same graph. This function is used to plot all my data in my spreadsheet. Doesn't make sense why this woudl fail after a certain number of time. any help? Thanks!

VB Code:
  1. Public Function plotGraph(ByVal mychart As Chart, _
  2.                         ByVal xaxisCol As Integer, ByVal secaxisCol As Integer, _
  3.                         ByVal avgcol As Integer, ByVal stdcol As Integer, _
  4.                         ByVal startRow As Integer, ByVal endRow As Integer, _
  5.                         ByVal appOpt As Boolean, ByVal sheetname As String, ByVal startprimrow As Integer)
  6.     Static commcount As Integer
  7.     Static primcount As Integer
  8.     Static avgcount As Integer, stdcount As Integer
  9.     Static saveInitRow As Integer, primRowCount As Integer
  10.     Dim x As Integer, row As Integer, labelrow As Integer
  11.     Dim storeValues()
  12.     labelrow = 6
  13.    
  14.     With mychart
  15.         .Axes(xlCategory, xlSecondary).TickLabelSpacing = 1
  16.         .Axes(xlCategory, xlPrimary).TickLabelSpacing = 1
  17.          
  18.         With .Axes(xlCategory, xlSecondary).TickLabels.Font
  19.             .Name = "Arial Narrow"
  20.             .FontStyle = "Regular"
  21.             .Size = 6
  22.         End With
  23.        
  24.         With .Axes(xlCategory, xlPrimary).TickLabels.Font
  25.             .Name = "Arial Narrow"
  26.             .FontStyle = "Regular"
  27.             .Size = 6
  28.         End With
  29.        
  30.         'plot secondary x axis values
  31.         .SeriesCollection(2).XValues = ""
  32.         .SeriesCollection(2).XValues = Range(Sheets(sheetname).Cells(startRow, secaxisCol), Sheets(sheetname).Cells(endRow, secaxisCol))
  33.                 '"=" & sheetname & "!R" & startRow & "C" & secaxisCol & ":R" & endRow & "C" & secaxisCol
  34.        
  35.         'plot primary x axis values
  36.         .SeriesCollection(1).XValues = ""
  37.         .SeriesCollection(1).XValues = Range(Sheets(sheetname).Cells(startRow, xaxisCol), Sheets(sheetname).Cells(endRow, xaxisCol)) 'errors here
  38. end with