The following example uses a Visual Basic array to load the chart data grid directly. To try the example, draw a MSChart control on a form, paste the code into the Form object's code module, and run the project.
Option Explicit
Option Base 1
Private Sub Form_Load()
Dim arrData(3, 1 To 3)
arrData(1, 1) = "Jan" ' Set the labels in the first series.
arrData(2, 1) = "Feb"
arrData(3, 1) = "Mar"
arrData(1, 2) = 8
arrData(2, 2) = 4
arrData(3, 2) = 0.3
arrData(1, 3) = 0.2
arrData(2, 3) = 3
arrData(3, 3) = 6.3
MSChart1.ChartData = arrData
End Sub
In this example, the lower subscript bound was declared as 1 using the Option Base 1 statement, rather than the default of 0. We use a Variant array where the first series values are set to string variables and the second and third series' values are set to numeric values. This allows both the chart's labels and data to be set simultaneously. Note that declaring the array as type String works too, as long as the second and third series contain numeric values. If you wish only to set the chart's data, the array can be of the numeric types Integer, Long, Single or Double. Note that doing this will replace the existing chart labels with default row/column labels. Also, a one-dimensional array will work as well as a two-dimensional one as long as the last values are either numeric or text representations of numeric values.
The following example returns data from the chart. The example contains a loop to print out the array returned from the chart. Note the use of the Lbound and Ubound functions to determine the array bounds from the chart. To try the example, draw a CommandButton control onto a form with a MSChart control. Paste the code into the code module, run the project, and click the button.
Option Explicit
Private Sub Command1_Click()
Dim y() As Variant
Dim i, j As Integer
y = MSChart1.ChartData
For i = LBound(y, 2) To UBound(y, 2)
Debug.Print
For j = LBound(y, 1) To UBound(y, 1)
Debug.Print y(j, i)
Next j
Next i
End Sub
The returned array lower bound values are equal to 0. The returned array will always be a two-dimensional array of type Variant. Since ChartData is the default property for the chart, the object name alone, such as MSChart1, can be substituted for MSChart1.ChartData. So you could use MSChart1 = data or data = MSChart1.