0

I'm trying to plot ranges to a plot which is already created on a worksheet. In the code below it is called chart1. The problem is that Xvals and Yvals are arrays of ranges. I need to plot a certain range in each array as shown. The error I get at that "NewSeries.Values = TempX " line is "RunTime error '5': Invalid procedure call or argument". tempX and tempY are defined as variant. I've also tried defining them as range. I've also tried assigning Xvals(BoxNum) directly to the .Xvalues. BoxNum is an integer corresponding to the spot in the array to plot. Any ideas what I'm doing wrong? Thanks in advance!

    series.NewSeries
    Set TempX = Xvals(BoxNum)
    Set TempY = Yvals(BoxNum)
    Set NewSeries = chart1.SeriesCollection(chart1.SeriesCollection.Count)
    NewSeries.Name = "Adjusted"
    NewSeries.Values = TempX
    NewSeries.XValues = TempY
3
  • Do you have those last two assignments flipped? Commented Apr 11, 2014 at 18:12
  • ah. I do. I modified the code to be forum friendly. It shouldn't matter, though, for the issue that is occurring. correct? Commented Apr 11, 2014 at 18:23
  • I don't know if it would matter: it might depend on what's already plotted for example. I think your code is a bit out of context (what is series for example?) but it looks basically OK to me, so it's hard to say what the issue might be without seeing a more-complete set of code. Commented Apr 11, 2014 at 20:36

1 Answer 1

1

I'm not sure what you want to do. Question is not clear enough, but the following code may suit your purpose. Why not use with statement to simplify code? Also, I'm not sure why you have to use TmpX and TmpY, instead directly applying values to the chart.

Sub setArrayToChart()
   Dim chat1 As Chart
   Dim Xvals(2) As Range, Yvals(2) As Range
   Dim boxNum As Integer

   Set chart1 = Charts("chart1")

   Set Xvals(0) = Range(Cells(1, 1), Cells(3, 1))
   Set Xvals(1) = Range(Cells(1, 2), Cells(3, 2))

   Set Yvals(0) = Range(Cells(1, 3), Cells(3, 3))
   Set Yvals(1) = Range(Cells(1, 4), Cells(3, 4))

   boxNum = 1

   Set TempX = Xvals(boxNum)
   Set TempY = Yvals(boxNum)

   With chart1.SeriesCollection.NewSeries
      .Name = "Adjusted"
      .Values = TempX
      .XValues = TempY
   End With
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you kwiqry. I didn't mention that the arrays were global arrays. you're answer was perfect because I didn't know if I could assign an array stored range to a Xvalues or .values. So I looked elsewhere and found issues in my array assignments which fixed the issue. Thank you to Tim Williams as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.