| Start |
|---|
| Copying Arrays You can copy a VBA array into an array of the same type using the The source array can be fixed or dynamic, but the destination array must be dynamic. Trying to copy to a fixed array will throw a "Can't assign to array" compilation error. Any preexisting data in the receiving array is lost and its bounds and dimenions are changed to the same as the source array. Once the copy is made the two arrays are seperate in memory, i.e. the two variables are not references to same underlying data, so changes made to one array do not appear in the other. Copying Arrays of ObjectsWith arrays of objects the references to those objects are copied, not the objects themselves. If a change is made to an object in one array it will also appear to be changed in the other array - they are both referencing the same object. However, setting an element to a different object in one array won't set it to that object the other array. Variants Containing an ArrayYou can also copy an array into and from a variant variable. When copying from a variant, it must contain an array of the same type as the receiving array otherwise it will throw a "Type mismatch" runtime error. |
| Returning Arrays from Functions A function in a normal module (but not a Class module) can return an array by putting The result of the function can then be put into a dynamic array of the same type or a variant. The elements can also be accessed directly by using a second set of brackets, however this will call the function each time, so its best to store the results in a new array if you plan to use them more than once Note that what is returned is actually a copy of the array inside the function, not a reference. So if the function returns the contents of a Static array its data can't be changed by the calling procedure. Outputting an Array via an output argumentIt is normally good coding practice for a procedure's arguments to be inputs and to output via the return value. However, the limitations of VBA sometimes make it necessary for a procedure to output data via a Outputting to a fixed arrayOutputting an Array from a Class methodAn output argument can also be used to output an array from a method/proceedure in a Class module |
| Passing Arrays to Proceedures Arrays can be passed to proceedures by putting Arrays must be passed by reference. If no passing mechanism is specified, e.g. Passing by reference means that any changes to the array will be preserved in the calling proceedure. If you want to avoid changing the original array then be careful to write the function so that it doesn't change any elements. Alternatively create a working copy of the array and work with the copy. |
Read Article
https://develop.horje.com/learn/1434/reference