VBA Change a 1 column array to a one dimensional array - arrays

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.

Related

Returning from an array

Maybe this has been asked before, but I don't even know what to search.
I have an array in Excel of n items.
I also have a binary array of n items and for each 1 in that binary array, I want to return the item its position corresponds to in the original array. For example, let's say I have: {A, B, C, D} and {0,0,1,1}. I want to return only C and D since those are the places that have 1s.
Is there a way to do that in Excel?
Thanks.
Best,
Morris
You can use:
=INDEX({"A","B","C","D"},N(IF({1},MODE.MULT(IF({0,0,1,1},COLUMN($A:$D)*{1;1})))))
Depending on ones version this may need to be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode.
My version automatically spills arrays so putting it in A1 gives me the full array output spilled down.
Try the following User Defined Function:
Public Function xtract(s1 As String, binpatrn As String) As String
Dim arr1, arr2, i As Long
arr1 = Split(s1, ",")
arr2 = Split(binpatrn, ",")
For i = LBound(arr1) To UBound(arr1)
If arr2(i) = "1" Then
xtract = xtract & "," & arr1(i)
End If
Next i
If xtract = "" Then
Exit Function
Else
xtract = Mid(xtract, 2)
End If
End Function
We place the comma-separated list (without braces) in A1 and the binary list (without braces ) in B1. In C1 enter:
=xtract(A1,B1)

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"

1D Array into 2D array (vb)

I have split the 1d array and i need convert it into a 2d array. The array consists of numbers (scores) and names as results of a numerical test.
Dim results1 As String = File.ReadAllText("Z:\scores class 1.txt")
Dim array = Split(results1, " ")
For i As Integer = 0 To array.Length - 1
Console.WriteLine(array(i))
Next
Console.WriteLine("Would you like these to be sorted? Press 1 for yes, 2 for no")
If Console.ReadLine = 1 Then
' do some stuff
ElseIf Console.ReadLine = 2 Then
' do some stuff
End If
Console.ReadLine()
That is my current code, what do i need to add?
Thanks for any help.
Converting 1D to 2D is possible when you have contigious key/value present to make it as index of array but in your case it is neither. So better option will be to use HashMap/HashTable in general. As per your requirement instead of using 2D array to represent name: value pair you should use a HashMap .
As per your statement in commnets you have an 1D array which holds name:value
i.e
String arr [] = { "jack", "23", "mat","45", "mike","56" }
You can better represent it as a HashMap .
HashMap<String,Integer> hm = new HashMap<String,Integer>();
hm.add("jack",23); // it will add score of 23 to name jack i.e "jack" : 23
hm.add("mat",45);
hm.add("mike",56);
Now you can easily manipulate any name:value pair
e.g. To get marks scored by "mat" just write hm.get("mat")
It will output the marks associated with mat i.e 45.
Dim array2((array.Length / 2) - 1, 1) As String
For i As Integer = 0 To (array.Length - 1) / 2 Step 1
array2(i, 0) = array(i * 2)
array2(i, 1) = array((i + 1) * 2 - 1)
Next
This creates a two dimensional array with the values from the 1d array.

declaring two-dimensional array

I have a couple of college assignments I am having trouble with. Really I am just confused about one thing regarding an array. I need to declare a three column, 5 row array. The first two columns are integers and the third column is the letter grade. So I am very confused about declaring the data type since they are different. This is my first go-around with arrays, so please excuse my ignorance. Here is an what my array is supposed to look like.
Column 1 {0,300,350,400,450}
Column 2 {299,349,399,449,500}
Column 3 {F,D,C,B,A}
(It's a grading app)
I can solve the rest of the problem myself, I am just confused about this array portion. So my question is strictly about how to declare such an array. It say's to use a two-dimensional array which only confuses me more since there are three columns. Thank you!
2-dimensional array is correct. First index is column, second index is row.
Dim strData(,) As String 'Use String variable type, even for the numbers
Dim intRowCount As Integer = 5
Dim intColumnCount As Integer = 3
ReDim strData(intColumnCount - 1, intRowCount - 1) 'subtract 1 because array indices are 0-based. Column 0 = Range start, Column 1 = Range End, Column 2 = Grade
'first row
strData(0, 0) = "0" 'Range start
strData(1, 0) = "299" 'Range end
strData(2, 0) = "F" 'Grade
'second row
strData(0, 1) = "300"
strData(1, 1) = "349"
strData(2, 1) = "D"
'third row
strData(0, 2) = "350"
strData(1, 2) = "399"
strData(2, 2) = "C"
'fourth row
strData(0, 3) = "400"
strData(1, 3) = "449"
strData(2, 3) = "B"
'fifth row
strData(0, 4) = "450"
strData(1, 4) = "500"
strData(2, 4) = "A"
'Add a row
intRowCount = intRowCount + 1
ReDim Preserve strData(intColumnCount - 1, intRowCount - 1)
'sixth row
strData(0, 5) = "501"
strData(1, 5) = "600"
strData(2, 5) = "A+"
Note that Redim Preserve can only change the last index in the array, which is why we store in (column, row) order rather than the more traditional (row, column) order.
There are a couple of ways to approach this. One is to declare the array as Object type, and they assign integers or strings to the appropriate element. Some don't consider this socially acceptable, though, because it can lead to code that's difficult to debug.
You could also use a String type for a two dimensional array and save the integers in string variables. This is also not normally done because of the conversion necessary for numeric comparisons and computation.
Another approach is to use a structure or class that contains the three values, and make an array of that.
For example,
Structure Item
Dim col1 as integer
Dim col2 as integer
Dim col3 as string
End Structure
Dim itemList(20) as Item
itemList(4).col1 = 23
itemList(4).col2 = 45
itemList(4).col3 = "somestring"

Resources