Cycle through variables incremented by a digit - arrays

I have an Access form where a bunch of sections are repeated, ex: Name, Age, Gender. The variable names for these text boxes are: Name1, Name2, ...; Age1, Age2, ...; etc.
I want to store all these values in an Array after a button is pressed. Instead of hardcoding all the variable names, I was wondering if there is a way to cycle through these in a for loop e.g.:
For i = 1 to 4
ArrayName(i) = ("Name" & i).value
ArrayAge(i) = ("Age" & i).value
ArrayGender(i) = ("Gender" & i).value
next i
I can get the string Name1 by ("Name" & i) but when I add .value after it, it doesn't seem to work.
I have also tried storing the variable name as a string into another array and then trying to use that array element to get the value, but that doesn't work either.

The error is because code is just building a string "Name1" and string does not have Value property. Consider:
Me.Controls("Name" & i).Value
or simplify with:
Me("Name" & i)
Value is default property so don't have to reference. However, the Me qualifier is important, otherwise the array will just populate with the control's name "Name1", not the control's value.
Arrays are 0-base by default which means the element index begins with 0 so iterating 1 to 4 to reference array elements will not provide correct results. Consider:
ArrayName(i - 1) = Me("Name" & i)
Otherwise, declare arrays as 1-base with Option Base 1 in module header.
Instead of 3 1-dimension arrays, a single 2-dimension array might serve.
Dim aryD(4, 3) As Variant, x As Integer, y As Integer
For x = 1 To 4
For y = 1 To 3
aryD(x - 1, y - 1) = Me(Choose(y, "Name", "Age", "Gender") & x)
Next
'verify array elements values
Debug.Print aryD(x - 1, 0) & " : " & aryD(x - 1, 1) & " : " & aryD(x - 1, 2)
Next

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.

Adding static strings to a cumulative Multi-dimension array

I have a REGEX loop that finds certain strings (the Match.value in the code below). I need to preform this loop 4 times, with 4 different REGEX's, and cumulatively build the array as it finds matches in each of the four REGEX loops.
I've been able to populate a 1D array with my matches but I cannot figure out how to add other information (all static text/strings that are associated with each match) to the other dimensions. I looking to end up with something like this:
Match.value | String1_here | String2_here | String3_here | String4_here
Dim serialArray() As String
For Each Match in theMatches
Redim Preserve serialArray(x)
serialArray(x) = Match.value
x = x + 1
Next Match
Try the following...
With theMatches
If .Count > 0 Then
ReDim serialArray(1 To .Count, 1 To 5) 'allocate storage space for an N X 5 array
x = 1
For Each Match In theMatches
serialArray(x, 1) = Match.Value
serialArray(x, 2) = "String1"
serialArray(x, 3) = "String2"
serialArray(x, 4) = "String3"
serialArray(x, 5) = "String4"
x = x + 1
Next Match
End If
End With

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)

Functions and loops

I am trying to make the function _EncryptionProcess() take arrays 1 by 1 and process them. I realized you can't have functions inside For loops.
The location in which the arrays need to be taken is where $aArray is typed, the arrays are stored in this value. The other variable defines the key size and value.
;Cuts the input up into piece;
$VariableToBeCut = "12345678"
$aArray = StringRegExp($VariableToBeCut, ".{2}", 3)
MsgBox(0, "die", $aArray[0]) ; personal check to make sure array works
$DataToBeEncrypted=_EncryptionProcess($aArray, $keyvalue, $keysize, 1) ;$aArray needs to be where the different arrays are processed
MsgBox(0, "Encrypted data", $DataToBeEncrypted)
This is how you should process array elements.
;Cuts the input up into piece;
$VariableToBeCut = "12345678"
$aArray = StringRegExp($VariableToBeCut, ".{2}", 3)
ConsoleWrite("Array element 0: " & $aArray[0] & #LF) ; personal check to make sure array works
For $i = 0 To UBound($aArray)-1
$DataToBeEncrypted = _EncryptionProcess($aArray[$i], $keyvalue, $keysize, 1)
ConsoleWrite("Element " & $i & " : " & $aArray[$i] & " DataToBeEncrypted: " & $DataToBeEncrypted & #LF)
Next

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