简体   繁体   中英

How do you use a two dimensional array to plot a graph in VBA Excel?

I am currently trying to create a dynamic graphing code that reads in 5 arrays and plots them on a scatter line graph. I have made the code and read in the 5 arrays.

I do not know how to apply the data that I have to plot in Excel. I am accomplishing this task in a function. Here is what I have currently:

Function Graph(ByRef int1 As Variant, ByRef int2 As Variant, ByRef int3 As Variant, _
               ByRef int4 As Variant, ByRef int5 As Variant)

Dim c As Chart
Dim srs As Series
Dim xValues As Variant
Dim yValues As Variant

Set c = ActiveChart
'Set srs = cht.SeriesCollection.NewSeries

xValues = Application.Index(int1, 0, 1)
yValues = Application.Index(int1, 0, 2)

With ActiveChart.SeriesCollection
    .xValues = xValues
    .Values = yValues
End With

End Function

I am trying to use a previously existing graph. The code needs to be able to use the currently selected graph and plot the data as indicated.

Let me know if you need any additional information.

Additional information: I am starting with a blank graph which will accept the arrays as series information (which will in turn populate the graph). If it is easier, I could use VBA to create a chart. I am trying to avoid having to edit the series ranges by hand. I am trying to avoid this because Excel graphs "blank cells" controlled by an equation. I have tried making graphs with large series ranges, but Excel graphs all values. Excel绘制“空白单元格”

This is even considering the check box that says do not graph blank cells.

I have a main function that reads in values and handles the main code. I am going to tie this macro to a button embedded in the spreadsheet.

Something like this:

With ActiveChart.SeriesCollection.NewSeries
    .xValues = xValues
    .Values = yValues
End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM