0

So, I have created an array, and I am trying to put the data into a chart... but I have no clue how to make the chart take on the data from an array I generated, rather than from a range. I coded for a chart uding a Range from an excel sheet.. but I can't see how to make all of that happen when I have this array..

I have two sub procedures. Inside one of them I want to use

Call ChartNew2(myArray)

How can I do that? I tried in this way and I wasn't successful either...

Sub ChartNew2(result2 As Variant)
Dim i As Integer
ReDim result2(1 To 4, 1 To 1)
Charts.Add
    For i = LBound(result2, 1) To UBound(result2, 1)
        result2(i, 1) = result2
    Next i
    ActiveChart.ChartType = xlXYScatterSmoothNoMarkers
    ActiveChart.Location Where:=xlLocationAsObject

    With ActiveChart
                .HasTitle = True
                .Axes(xlValue, xlPrimary).HasTitle = True
        End With


    With ActiveChart.Axes(xlValue)
                .HasMajorGridlines = True
    End With

    ActiveChart.HasLegend = False
    ActiveChart.PlotArea.Select

    Selection.Interior.ColorIndex = xlAutomatic
    ActiveChart.Axes(xlValue).Select

    With ActiveChart.Axes(xlValue)

            .MaximumScale = 1

    End With

End Sub
4
  • It generates a blank page.. Commented Apr 26, 2013 at 6:47
  • Is it acceptable to drop your array values on a page, create a range against it and get the chart to build from the range? Commented Apr 26, 2013 at 7:30
  • I wouldn't like to do it, unless I can do it and then let the values in those ranges disappear, cause they would look VERY ugly. Commented Apr 26, 2013 at 7:31
  • You can probably hide the worksheet with the values on it. Then you wouldn't have to behold its ugliness. Commented Apr 26, 2013 at 7:33

1 Answer 1

1

This link: http://www.excelforum.com/excel-charting-and-pivots/400227-making-charts-from-arrays-in-vba.html

says to use this syntax:

With ActiveChart.SeriesCollection(1)
    .XValues = MyXArray
    .Values = MyYArray
    .Name = MyName
End With

If you're having troubles passing arrays, I think this is the syntax if your array is an integer array.

Sub ChartNew2(result2() As Integer)
Sign up to request clarification or add additional context in comments.

9 Comments

Yes, I tried it but the problem is the array values are generated by a different Sub procedure... And then I call on them. I don't know how to refer correctly to that array...
So you've validated that that code works if the arrays are local? S the real problem is passing arrays, not using them in charts?
I actually have my array as a Variant, and it works as a Variant.. Integer wouldn't cause I don't have integers.
How do you chart a variant? Surely they are numbers of some kind. Anyway, does it work if you use Variant instead of Integer?
Oh now that I do some reading it seems you know more than me :). When you use your code as-is, what line does the error occur on?
|

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.