Table of Contents
- Arrays in VBA for Excel
- 5 Different Types of Arrays in VBA Excel
- How to Use Arrays in VBA Excel (with examples)
Arrays in VBA Excel
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.
Excel VBA 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 of using Arrays in VBA Excel is because of the fact that in 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 in excel that can hold the data in it. For creating an Array the data must be of the same type.
- The Excel VBA 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.
Top 5 Different Types of Arrays in VBA Excel
Below are the types of arrays in VBA Excel.
#1 – Static Arrays
An array that has Predefined count of value that can be stored in it.
#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
Excel 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 from one 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 the data is stored in these Excel VBA arrays they are ready to use as a variable in the VBA coding.
How to Use Arrays in Excel VBA (with examples)?
Excel VBA 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 arrays in VBA 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”.
Array in VBA Example #1
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”.
Array in VBA Example #2
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.
Array in VBA Example #3
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”
Array in VBA Example #4
Closing the code.
After the data has been entered for the array, the last step will be to close the code.
Things to Remember About Arrays in VBA Excel
- 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 “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.
You can download this Arrays in VBA Excel template here – Arrays in VBA Excel Template
This has been a guide to Arrays in Excel VBA. Here we discuss the different types of VBA Arrays, how to use Arrays in VBA Excel along with excel example and downloadable excel templates. You may also look at these useful excel tools –
- VBA Macros Online Course
- OFFSET Function Examples
- Top 6 VBA Books
- VLOOKUP with Many Criterias
The post Arrays in Excel VBA appeared first on Learn Investment Banking: Financial Modeling Training Courses Online.