Excel VBA Arrays
In general one variable is supposed to hold a single value at a time, but when we want to store multiple values in a single variable then such type of variable is known as array variable, to use an array variable in VBA we need to declare or define it first, we can define the array variable with its length or without its length.
Suppose if we have data that contains hundreds of rows and multiple columns and we need to create a code that will use the data. Now, in this case, we have to create multiples of the variable that will fetch the value from the cells and give to the program. This will be very tiring to create this much of variable and hence in such cases, we use Arrays in excel.
Arrays hold the data set in their memory and do not need us to declare the variable for each of the value that needs to be fetched from the data. The need for using arrays is because of the fact that in an excel variable are designed to hold one value at a time, however, when multiple values are stored by a variable it becomes an array.
- Creating an Array is like creating a separate memory unit that can hold the data in it. For creating an Array the data must be of the same type.
- Arrays that we give to excel must correspond to the type of data that we have. Suppose if we have data that has only rows so in this case, we will use the “One-dimensional array” and if data contains columns also then we have to use “Two-dimensional arrays” as they are only capable of holding the values from the rows and columns.
- Arrays also have to function to work as dynamic arrays or static arrays. As we give dynamic range to the formula we can also make the Arrays variable. Dynamic arrays will have the functionality to include infinity number of rows and columns. In case if the arrays that we have defined are of a static type then they can only hold limited numbers of rows and columns as defined at the time of creating the array.
Array work on the “Mathematical rule of matrix” that is they identify the data by its location only. Suppose if we have to make VBA understand that we need “20” in cell “B3” then we have to write the code of the location as (3, 2) where the first value stands for the location of row and second value stands for the column number. In excel world these code of locations is called “Upper bound” and “lower bound”. By default the location in excel starts from one and not from zero, so excel sees “A1” as row number 0 and not row number 1.
Similarly, the columns start from zero and not from one.
These arrays can be defined as a static array or dynamic array. If we define them as static array this means that they cannot hold more then the variables as defined while coding them. If we are not sure about the value that is required to be memorized by the arrays we create dynamic arrays and in such cases, they can hold infinity numbers of values.
Now after we have selected the type of array that is required we will now have to enter the data in these arrays.
This data has to be given one by one to excel in below ways.
After data is stored in these arrays they are ready to use as a variable in the VBA coding.
List of Top 5 Types of Arrays
- Static Arrays
- Dynamic Array
- One Dimensional Array
- Two Dimensional Array
- Multidimensional Array
Let’s see each one of them in detail.
#1 – Static Arrays
An array that has a Predefined count of value that can be stored in it.
4.6 (247 ratings) 3 Courses | 12 Hands-on Projects | 43+ Hours | Full Lifetime Access | Certificate of Completion
#2 – Dynamic array
Array with a not predefined count of value that it can handle.
#3 – One-dimensional Array
An array that can hold only data either from rows or columns.
#4 – Two-dimensional Array
An array that can store a value from the rows and columns.
#5 – Multidimensional Array
How to use Arrays in VBA (with examples)?
Arrays can be used in many situations but they must be used when the number of variables that are to be declared is large in number and it is not feasible to declare them.
Below are some of the examples but before going to the examples we will learn to open the VBA editor by shortcut key
This will open the VBA editor, from there we have to enter the code in “This worksheet”.
Choose the type of array that you want, should it be a dynamic or static array?
If we need a dynamic array then we will define the dimension as “variant”.
If we need a static array then we will define a dimension as “Static”.
Define the columns and rows that you want the array to store.
If we have entered “1” in the bracket this means that array can hold 2 rows value as excel counting starts from zero.
If we need columns and rows also then we need to define both of them.
Here “1 to 2” means that two rows and “1 to 3” means that three columns.
Here we have changed the rule of how excel counts rows and asked it to count from “1” and not from zero.
The input of data in the array.
The data has to entered cells wise. Here the data is to be entered in form of (I,j) where “I” means the row and “J” means the column.
So “a (1,1”) means that cell ”A1”
Closing the code.
After the data has been entered for the array, the last step will be to close the code.
Things to Remember
- By default, Excel will count the rows starting from zero. This means that “2” in place of “I” will means 3 rows and not 2 rows. This same applies to “J”.
- The data that has to be entered for the array must be started from (0, 0) that is from the first row and first column.
- In case if we are using the dynamic arrays then this will need the function of “VBA REDIM” to define the number of rows and columns that are required to be memorized.
- In case of creating a two-dimensional array then we have to use “Integer” as the dimension.
- The excel file needs to be saved in the version that is “Macro compatible” else the coding that we have done in VBA will vanish and will not run the next time.
This has been a guide to VBA Arrays. Here we discuss the different types of Arrays, how to declare and use them along with examples and downloadable templates. You may also look at these useful excel tools –