Dynamically Dimensioning A VBA Array? - arrays

Why am I unable to set the size of an array based on a variable? What's the best way around this?
Dim NumberOfZombies as integer
NumberOfZombies = 20000
Dim Zombies(NumberOfZombies) as New Zombie

You can use a dynamic array when you don't know the number of values it will contain until run-time:
Dim Zombies() As Integer
ReDim Zombies(NumberOfZombies)
Or you could do everything with one statement if you're creating an array that's local to a procedure:
ReDim Zombies(NumberOfZombies) As Integer
Fixed-size arrays require the number of elements contained to be known at compile-time. This is why you can't use a variable to set the size of the array—by definition, the values of a variable are variable and only known at run-time.
You could use a constant if you knew the value of the variable was not going to change:
Const NumberOfZombies = 2000
but there's no way to cast between constants and variables. They have distinctly different meanings.

You have to use the ReDim statement to dynamically size arrays.
Public Sub Test()
Dim NumberOfZombies As Integer
NumberOfZombies = 20000
Dim Zombies() As New Zombie
ReDim Zombies(NumberOfZombies)
End Sub
This can seem strange when you already know the size of your array, but there you go!

You can also look into using the Collection Object. This usually works better than an array for custom objects, since it dynamically sizes and has methods for:
Add
Count
Remove
Item(index)
Plus its normally easier to loop through a collection too since you can use the for...each structure very easily with a collection.

You need to use a constant.
CONST NumberOfZombies = 20000
Dim Zombies(NumberOfZombies) As Zombies
or if you want to use a variable you have to do it this way:
Dim NumberOfZombies As Integer
NumberOfZombies = 20000
Dim Zombies() As Zombies
ReDim Zombies(NumberOfZombies)

Related

Defining a multidimensional array in excel with calculated values (they're not a constant) [duplicate]

Why am I unable to set the size of an array based on a variable? What's the best way around this?
Dim NumberOfZombies as integer
NumberOfZombies = 20000
Dim Zombies(NumberOfZombies) as New Zombie
You can use a dynamic array when you don't know the number of values it will contain until run-time:
Dim Zombies() As Integer
ReDim Zombies(NumberOfZombies)
Or you could do everything with one statement if you're creating an array that's local to a procedure:
ReDim Zombies(NumberOfZombies) As Integer
Fixed-size arrays require the number of elements contained to be known at compile-time. This is why you can't use a variable to set the size of the array—by definition, the values of a variable are variable and only known at run-time.
You could use a constant if you knew the value of the variable was not going to change:
Const NumberOfZombies = 2000
but there's no way to cast between constants and variables. They have distinctly different meanings.
You have to use the ReDim statement to dynamically size arrays.
Public Sub Test()
Dim NumberOfZombies As Integer
NumberOfZombies = 20000
Dim Zombies() As New Zombie
ReDim Zombies(NumberOfZombies)
End Sub
This can seem strange when you already know the size of your array, but there you go!
You can also look into using the Collection Object. This usually works better than an array for custom objects, since it dynamically sizes and has methods for:
Add
Count
Remove
Item(index)
Plus its normally easier to loop through a collection too since you can use the for...each structure very easily with a collection.
You need to use a constant.
CONST NumberOfZombies = 20000
Dim Zombies(NumberOfZombies) As Zombies
or if you want to use a variable you have to do it this way:
Dim NumberOfZombies As Integer
NumberOfZombies = 20000
Dim Zombies() As Zombies
ReDim Zombies(NumberOfZombies)

VBA Access 2010 extracting array elements as individual dynamically named variables

I am working through an Access 2010 VBA script. I am pulling information from a temporary table that may have 1 to 10 records in it. I am using GetRows to build a two-dimensional array that looks like this, for example:
**0** **1**
8677229 1
10289183 2
11981680 3
13043481 4
I have tested the array function by debug print, and the array does contain values. I want to split out the array values dynamically into variables for later use. In this example, I would like the 0 column to produce 4 variables named Anum(0) through Anum(3), and the 1 column to produce 4 variables named Pay(0) through Pay(3). However, I keep getting a "Error 9, subscript out of range" where indicated below:
Dim db As Database
Dim rs As Recordset
Dim PayAcct As Variant
Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT Anum, Pay FROM PayPerAcct")
Dim highval As Integer
Dim i As Integer
i = 0
While Not rs.EOF
PayAcct = rs.GetRows(10)
Wend
highval = UBound(PayAcct, 2)
Dim Anum() As Variant
Dim Pay() As Variant
For i = 0 To highval
'error occurs on the below line
Anum(i) = CStr(PayAcct(0, i))
Pay(i) = CStr(PayAcct(1, i))
Next i
When I manually define Anum and Pay variables and use the Cstr(PayAcct(1,0)) operation, it passes the expected value from the array to the variable, so I don't think that is the problem.
I suspect I am assigning Anum() and Pay() the wrong dimension, because I have seen similar example code in Excel VBA where defining those variables as Range works. I have tried defining Anum() and Pay() as Range (which doesn't work, because this is Access VBA), Variant, and Object.
Any thoughts or tips?
Edit -
The below ended up working, thanks for your help:
Dim Anum() As String
ReDim Anum(0 To highval)
Dim Pay() As String
ReDim Pay(0 To highval)
Dim Anum() As Variant
declares a dynamic array that hasn't any elements yet, so you can't assign values to it. It needs a ReDim to use it.
But since you already know how many elements you need, what you want is:
Dim Anum(0 To highval) As Variant
or if you know that you will only store strings in it,
Dim Anum(0 To highval) As String

Declaring a dynamic array not working as expected

RsProxyList.Open objDBCommand,,1,1
dim recCount:recCount = RsProxyList.RecordCount
Dim output(recCount,2)
I get an error because recCount is of wrong type. I have tried to convert it to Int but that does not work either. The following works fine:
RsProxyList.Open objDBCommand,,1,1
dim recCount:recCount = RsProxyList.RecordCount
Dim output(3,2)
How do I convert recCount to get this array declaration to work?
You need to first declare your Array as dynamic then use ReDim to set the first dimension dynamically, like this;
Dim output() 'Declare a "Dynamic Array"
ReDim output(recCount, 2) 'Set dynamic first dimension.
By specifying the dimensions like this;
Dim output(10, 2) 'Declare a "Fixed Array"
you tell VBScript to build a fixed array which does not accept variables as dimension declarations.
Dim output() versus Dim output
There seems to be some argument as to using
Dim output()
versus
Dim output
#Bond in the comments below came up with the most convincing one in my opinion for using Dim output().
Because VBScript stores all variables as Variant data type declaring an Array using Dim output is just like declaring any other variable, whereas Dim output() creates an Array (0 sized array but an array nonetheless), this means there is one significant difference as far as the VBScript Runtime is concerned, Dim output() is an Array.
What does this matter?, well it matters because functions like IsArray() will still detect the variable as an Array where as "arrays" declared using Dim output will return False.
Useful Links
#DougGlancy's answer to Redim without Dim? (vba but still relevant).
VBScript Reference - Dim Statement
VBScript Reference - VBScript Variables (specifically Array Variables sub heading).

vba, Why an un-dimensioned array works with Split()

I can use an un-dimensioned String array with the Split() function to read fields from a String, but apparently, a String array has to be dimensioned to use it in a loop.
Why is that, and are there other situations where an array does not need to be dimensioned?
Dim field() As String
field = Split(data_line, "~")
Dim pref_line(10) As String
Input #1, pref_line(i)
but apparently, a String array has to be dimensioned to use it in a loop.
When use an un-dimensioned String array with the Split() function, the array is automatically dimensioned and values assigned to it. Also you can use that in a loop using For i = LBound(field) to UBound(field)
Option Explicit
Sub Sample()
Dim field() As String
Dim data_line As String
data_line = "aaa~bbb"
field = Split(data_line, "~")
Debug.Print field(0)
End Sub
Are there other situations where an array does not need to be dimensioned?
Yes. When you do not know how many items needs to be added to an array then you declare and un dimensioned array and Redim Preserve it to add values. For example
Option Explicit
Sub Sample()
Dim field() As Long
Dim n As Long, i As Long
ReDim Preserve field(n)
For i = 1 To 100
field(n) = i
n = n + 1
ReDim Preserve field(n)
Next i
End Sub
Split() returns its own array regardless of what you defined before. You are effectively overwriting the value of your variable.
Dim field() As String declares an array, but does not allocate it.
Dim pref_line(10) As String both declares and allocates it.
field = Split(data_line, "~") both allocates an array and populates it.
Others have already answered your question, and correctly about what you asked for. So I won't comment on that.
On a side note, you should avoid Redimming arrays as much as possible. Do that only when absolutely necessary. Remember that it is just one line of code for us, but a whole lot of work for the runtime engine. First find a new contiguous memory location for the new array; then copy the array items from the old to new one; then discard the old array. So you see one Redim Preserve statement produces how much work for the compiler.
For this reason, redimming inside a loop is "usually" a bad idea, since you know in advance how many times the loop will run and how many elements you will have in the end. So in such cases, redim your array before the start of loop, rather than doing it inside the loop. The end result would be a better performance :)
Taking the code in #SiddharthRout 's reply as an example, notice that the Loop will produce 100 items since you are looping from 1 to 100. So instead of ReDim Preserve field(n) inside the loop, move that outside it before the start of loop. ReDim Preserve field(100) as Long, or simply ReDim field(100) as Long.

Array in excel vba

I want to have an array list in vba, hence I have a variant declared in excel vba like:
Dim Students(10) as variant
Now I want to store numbers in Students list. the numbers are not continuous. Sometime like:
Students(2,7,14,54,33,45,55,59,62,66,69)
How can I do this in vba? also how can I access the list items?
Students must be declared as a dynamic array. That is, an array whose bounds can be changed. Dim Students(10) gives an array whose bounds cannot be changed and cannot be loaded from an array.
Dim Students() As Variant
To load Students:
Students = Array(2,7,14,54,33,45,55,59,62,66,69)
To access the elements:
Dim Inx As Long
For Inx = LBound(Students) to UBound(Students)
Debug.Print Students(Inx)
Next
LBound (Lower bound) and UBound mean that the for loop adjusts to the actual number of elements in Students.
This is too complex for you right now, and you'll probably never run into a situation where you'll need this, but:
I use the following method for forming more memory-efficient arrays (because Variant uses the most memory of any variable type) while still having the convenience of declaring the array contents in one line. To follow your example:
Dim Students() As Long
Dim Array2() As String
Array2() = Split("2,7,14,54,33,45,55,59,62,66,69", ",")
ReDim Array1(0) As Long
For Loop1 = LBound(Array2()) To UBound(Array2())
ReDim Preserve Array1(0 To (UBound(Array1) + 1)) As String
Array1(Loop1) = Array2(Loop1)
Next Loop1
ReDim Preserve Array1(0 To (UBound(Array1) - 1)) As Long
Erase Array2
An example of accessing it would be something like:
For Loop1 = LBound(Students) to UBound(Students)
Msgbox Students(Loop1)
Next Loop1
I learned this from here: http://www.vbforums.com/showthread.php?669265-RESOLVED-VBA-Excel-Assigning-values-to-array-in-a-single-line&p=4116778&viewfull=1#post4116778
You can add values to an Array like this...
For i = 1 to 10
Students(i) = i
Next i
Or like this
Students = Array(2,7,14,54,33,45,55,59,62,66,69)
Then you can access the values in the same manor. Note if you use the second option you'll need to declare it as follows:
Dim Students() As Variant
Well,
That depends on how you would supply the values for the array, would you get the values from Worksheet.Range or from TextBox or ListBox , But basically the code would be something like that :
Dim students(10) as Integer
Dim Carrier as Integer
For i = LBound(students) To UBound(Students)
'some code to get the values you want to from whatever is your source
'then assign the value to Carrier
students(i)=Carrier
Next i
It is not good practice to dim an array as Variant when you certainly know that you are going to use integers only, as it will eat alot of memory that is not needed in the first place.
You also should be aware of the bounds of the numbers that are going to be assigned, if it exceeds the Integer limit then you should use Double or Float.
This is my first participation in the site,Cheers.

Resources