I am a little confused about how I should plot arrays. I am often confused with Charts and Chart Objects. I would like to pass arrays into a function that plots the arrays. In the main code getPlots I would like to make each column an array and divide it by the first column/array (I also need the first column divided by itself). In the plotting function, I often have an invalid parameter error, but I think my confusion comes from whether I am dealing with charts or chart objects. It happens in the plotting function in the line that says with activechart...
I should add that the timeArray represents the x-values.
Private Sub getPlots_Click()
Dim numberOfRows As Integer
Dim rowRange As Range
Dim i, m, seriesIndex As Integer
Dim firstArray() As Variant
Dim secondArray() As Variant
Dim timeArray() As Variant
If ActiveSheet.ChartObjects.Count <> 0 Then
ActiveSheet.ChartObjects.Delete
End If
i = 5 'column number
seriesIndex = 1
Set rowRange = Range(Cells(8, i), Cells(Rows.Count, i).End(xlUp))
numberOfRows = rowRange.Rows.Count + 7
firstArray = Range(Cells(9, i), Cells(numberOfRows, i)).Value
timeArray = Range(Cells(9, 4), Cells(numberOfRows, 4)).Value
If ActiveSheet.ChartObjects.Count = 0 Then
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatterLines
End If
While Cells(8, i) <> ""
Set rowRange = Range(Cells(8, i), Cells(Rows.Count, i).End(xlUp))
numberOfRows = rowRange.Rows.Count + 7
secondArray = Range(Cells(9, i), Cells(numberOfRows, i)).Value
Call createPlot(firstArray, secondArray, timeArray, seriesIndex)
i = i + 1
seriesIndex = seriesIndex + 1
Wend
End Sub
Function createPlot(firstArray As Variant, secondArray As Variant, timeArray As Variant, seriesIndex As Integer)
Dim normalizedArray() As Variant
Dim array1() As Variant
Dim array2() As Variant
Dim time() As Variant
Dim yArray() As Double
Dim m, i, size, index As Integer
array1 = firstArray
array2 = secondArray
time = timeArray
index = seriesIndex
i = 0
size = UBound(array1, 1)
ReDim yArray(size)
For m = LBound(array1, 1) To UBound(array1, 1)
yArray(i) = array2(m, 1) / array1(m, 1)
i = i + 1
Next m
If ActiveSheet.ChartObjects.Count <> 0 Then
ActiveSheet.ChartObjects.Select
End If
With ActiveChart.SeriesCollection(index)
.XValues = time
.Value = yArray
End With
End Function
Bookmarks