What is an Array?
An array is nothing but a group of values or elements of same data type store in a memory. All the grouped elements can have a common name. An array variable also called as an element. We can refer stored values from an array using array name or the array index number. An array elements are sequentially indexed. Each variable in an array has a unique identifying index number. Index number also called as subscript. We make changes to variable using Index number. When we make changes to any variable in an array, it doesn’t affect other variables.
Syntax of an Array:
Here is the syntax of an array.
Dim VariableName(ArrayDimension/Subscript) DataType
Let us see an example of basic one dimensional array.
Sub VBA_Basic_Array() 'Variable declaration Dim arrayName(1) String 'or Public arrayName(1) String 'or Private arrayName(1) String 'Assign values to array variables arrayName(0)=“TestString1” arrayName(1) )=“TestString1” End Sub
Usage of an Array:
There are different usages with an array.
- It is used to store lists of data items of same data type.
- All array elements can be viewed in the Watch window.
- It is easy to maintain and sort data in an array.
- It has better performance.
Please find the following link for types of arrays(Static & Dynamic).