Can I create a line graph from an Array? - arrays

I have 6 arrays of data as longs. I want to find a way to graph all six arrays as separate lines on a Chart Control.
I have created the Chart object, in my Series Collection Editor I have 6 members, but I cannot find a function that does something along the lines of chart1.series1.setData(myArray).
I know I can go through and call .add() on every point, but I am wondering if there is a direct way to assign an array to a Series.

There is no built in way to add a whole range apart from databinding. You can call, as you said, the AddXY method of the Points property (or one of the related methods in the type DataPointCollection) of the Series, like so:
'Add data from Array1 to the first series of the chart
Chart.Series(0).Points.Clear() 'Clear all points
For i = 0 to Array1.Count - 1
Chart.Series(0).Points.AddXY(i, Array1(i)) 'Adds the data from the array to the first series
Next
If you have the data in a multidimensional (e.g. Dim Data()() As Long) you can do something like
For a = 0 to Data.Count - 1
Chart.Series(a).Points.Clear() 'Clear all points from the ath series
For i = 0 to Data(a).Count - 1
Chart.Series(a).Points.AddXY(i, Data(a)(i)) 'Adds the data from the ath array to the ath series
Next
Next
Note the X values will be just an index (0 to the array's count - 1) since you provided no further information.
Or you could write an extension method in a new Module
Public Module Extensions
<System.Runtime.CompilerServices.Extension>
Public Sub AddRange(d As System.Windows.Forms.DataVisualization.Charting.DataPointCollection, data() As Long)
Dim meCount As Integer = d.Count
For i = 0 To data.Count - 1
d.AddXY(meCount + i, data(i))
Next
End Sub
End Module
Now you can just call the AddRange method for the Points property of the series:
Chart.Series(0).Points.AddRange(Array1)

You can do it by adding point by point as Jens mentions, and this works find. Another way which should work fine is:
For a as integer = 0 to AmountOfSeries.count - 1 step 1
Chart1.Series(a).Points.DataBindXY(ArrayX1, ArrayY1)
Next
This may be closer to what you were looking for - a way to set two arrays to a series.
Here's what it would look like with only one series:
Dim xs As Double() = {0, 1, 2, 3, 4}
Dim ys As Double() = {0, 1, 2, 3, 4}
Chart1.Series(0).Points.DataBindXY(xs, ys)

Related

VBA Change a 1 column array to a one dimensional array

I am loading an array from a table.
aryNonE = ActiveSheet.Range("AA1:AA" & lRowNonE - 1)
test = Array("Bob Smith", "John Davies"...)
Want to use Filter but get type mismatch on aryNonE.
test works fine
[Debug shows1
How do I get aryNonE to look like test??
Thanks
Try this way, please:
Sub test2DTo1DArray()
Dim aryNonE, lRowNonE As Long
lRowNonE = 10
aryNonE = ActiveSheet.Range("AA1:AA" & lRowNonE - 1) '2D array
aryNonE = Application.Transpose(Application.Index(aryNonE, 0, 1)) '1D array
'You can test it in this way:
Debug.Print Join(aryNonE, ",")
End Sub
In this way, it can be used to filter using an array like Criteria1...
But, if the values in the 2D array are numbers, since the Criteria array must keep only strings, the range where the array is extracted from, must preliminarily be formatted as text.

Excel VBA: Mysterious Zero is Being Added to Array Created with "For" Loop

Hey everyone: I'm trying to make a function that digs through an array and adds values from range2 when the corresponding value from range1 equals criteria1.
I'm relatively new to VBA, so it's not the most elegant function in the world, but here's my code:
Function SingleArray(range1 As Range, range2 As Range, criteria1 As String)
Dim newrange() As Double
Dim d As Integer
Dim g As Integer
Dim i As Integer
g = Application.WorksheetFunction.CountIf(range1, criteria1)
ReDim newrange(g)
d = 1
For i = 0 To (range1.Count)
If range1(i) = criteria1 Then
newrange(d) = range2.Item(i).Value
d = d + 1
End If
Next i
SingleArray = newrange
End Function
Here is my data sample:
range2 range1
-5000 Bob
-5000 Jim
200 Bob
500 Jim
5000 Bob
200 Bob
300 Bob
1000 Bob
When I set the criteria as "Bob," the array that is returned is as follows:
{0,-5000,200,5000,200,300,1000}
I'm genuinely at a loss for how that zero is making it in there. Any thoughts you can provide would be most welcome!
1-D arrays default to a zero-based index structure (e.g. 0, 1, 2, 3, ....). You are looping through the ranges with a one based index (e.g. 1, 2, 3, 4, ...).
When you declare ReDim newrange(5) you are actually creating an array with six elements, not five (e.g. 0, 1, 2, 3, 4, 5)
You can make all arrays on that code sheet default to a one based index by putting this compiler directive at the top of the code sheet.
Option Base 1
You can also change the way the array is declared on the fly by specifying the Lower Boundary and the Upper Boundary.
ReDim newrange(1 to g)

defining array from worksheet not working in filter () lines with type mismatch error

I'm using secondarray as range of cells in a worksheet (Ex. "1", "2") to exclude them as autofilter list that I'm defining in the below function in "filtercriteria".
I get "type mismatch" error in the filter (secondarray) line for some reason, but I works flawlessly when I define an array using a list of items instead. For example, if I use below line to define secondarray instead.
secondarray = ("1", "2")
I've researched similar postings and wasn't lucky, can someone help with this instance?
Thanks,
Dim secondArray As Variant
secondArray = Range("L76:M76").Value
c = 0
k = 0
count = 0
rowNumb = Worksheets("List").Range(Worksheets("List").Range("L5"), Worksheets("List").Range("L5").End(xlDown)).Rows.count
For L = 1 To rowNumb
c = Worksheets("List").Range("L5").Offset(L)
If c <> k Then
'check the current activity type against the array of types we don’t want. If it isn’t in the array we add it to an array that will be used as the filter criteria
If UBound(Filter(secondArray, c)) = -1 Then
ReDim Preserve filterCriteria(0 To count)
filterCriteria(count) = c
count = count + 1
End If
k = c
End If
Next
It isn't working because filter function takes a One-dimensional array of strings to be searched for its sourcearray argument.
When you read in a range from the sheet you automatically get a 2d array as opposed to the 1D you have when assigning from a list.
Find a way to use a 1D array to pass in
For example, as your data is coming from 1 row then slice the array by row
UBound(Filter(Application.WorksheetFunction.Index(secondArray, 1, 0), c)) = -1
You may need to find the right method for you.
Another method is given here.

Storing values into a multidimensional array

I am trying to Redim an array with multiple columns like:
Dim f() as Variant
ReDim f(0 To 0, 0 To 0) As Variant
ReDim Preserve f(0 To UBound(f), 0 To UBound(f))
To obtain something like this:
[15 34 70]
When I try to store into the array, I am doing as:
f(0,i) = ...
f(1,i) = ...
f(2,i) = ...
What's wrong with the code?
I wanted to create an array with 1 row and 3 columns where the number of columns could increase. First, I needed to re-dimension the empty array (Dim f() As Variant) as follows (ReDim f(0 To a, 0 To 3)). The above code was not working since I was trying to Redim the array that was already dimensioned.
Second, I wanted the array to return:
f(0,i) = "Bring the value from, for instance, cells A9"
f(0,i+1) = "Bring the value from, for instance, cells A16"
f(0,i+2) = "Bring the value from, for instance, cells G16"

Creating arrays in QTP

I am trying to create an array of integers in QTP ( the ints are 9, 16, 25,34,43). I think the code to instantiate it should be (but I could be wrong since I have never created an array in QTP before),
Dim pages(5)
pages(0) = 9
pages(1) = 16
...
Then I have a for loop with a variable that goes from 1 to 50 and based off of the value of the variable it does one thing and if the variable is one of the values in the array it does something else. For that I have,
For g = 1 to 50
if g<> 9 and g<> 16 and g<> 25 and g<>34 and g<> 43 Then
DoCoolStuff...
else
DoBoringStuff...
End If
Next
My question is, is there a command that will allow me to replace that ugly if statement with something like
if g <> in pages*?
If you want a Dimensioned Array, then that is the only way to declare an array. If you wanted a Non dimensioned array you then can use,
Dim pages()
pages = Array(9, 16, 25, 34, 43)
However, you can also do this,
Dim pages()
ReDim pages(5)
pages = Array(9, 16, 25, 34, 43)
Coming to your problem, you can get this going by using the Filter function. Although there is a very small problem. Filter method takes in String, so even with that function your will match 1, 2, 3, 4, 5, 6 along with the real/actual values 9, 16, 25, 34, 43.
As,
1 occurs in 16.
2 occurs in 25.
3 occurs in 34 and 43.
4occurs in 34 and 43.
5 occurs in 25.
6 occurs in 16.
It still thinks they occur in the String. One way to get around this is to Format the numbers as a two literal. Something like.
Dim pages(), g As Integer
ReDim pages(5)
pages = Array("09", "16", "25", "34", "43")
For g = 1 To 50
If UBound(Filter(pages, Format(g, "00"))) > -1 Then
'Do Cool Stuff here
Else
'Do Boring Stuff here
End If
Next
EDIT :
The other way is to create a User Defined Function that could Loop through your Array and find if the Value is Found in your Array. Something like,
Public Function FindArrayElement(SearchArray As Variant, LookupValue As Integer) As Boolean
Dim aCtr As Integer
For aCtr = 0 To UBound(SearchArray)
If CLng(SearchArray(aCtr)) = LookupValue Then
FindArrayElement = True
Exit Function
End If
Next
FindArrayElement = False
End Function
The function takes in two Arguments. The first is the Array in which the values are defined, the second is the Value looked up for. So your Original code would change to.
Dim pages(), g As Integer
ReDim pages(5)
pages = Array(9, 16, 25, 34, 43)
For g = 1 To 50
If FindArrayElement(pages, g) Then
'Do Cool Stuff here
Else
'Do Boring Stuff here
End If
Next
First, I, too, would suggest to initialize Pages like this:
Dim Pages(): Pages=(9,16,25,34,43)
Second, and independently from the first aspect, you could use this code to check if g is contained in Pages:
Dim Elem
Dim Found: Found=false
For Each Elem in Pages
If Elem = g then
Found=true
Exit For
End If
End For
If Found then
DoBoringStuff
else
DoCoolStuff
End If
The For..Each loop iterates as many times as there are elements in the Pages array. For each iteration, Elem is set to one Pages array element.
Note that the comparison is between Integers, as requested.

Resources