WallStreetMojo

WallStreetMojo

WallStreetMojo

MENUMENU
  • Free Tutorials
  • Certification Courses
  • Excel VBA All in One Bundle
  • Login
Home » Excel, VBA & Power BI » Excel Tutorials » VBA Arrays

VBA Arrays

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 them to the program. This will be very tiring to create this much of a variable, and hence in such cases, we use Arrays in excel.

Arrays in VBA 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 is 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 a dynamic range to the formula, we can also make the Arrays variable. Dynamic arrays will have the functionality to include an 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.

Explanation

Array works on the “Mathematical rule of a 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 the row and the 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.

Arrays in VBA Excel 1

These arrays can be defined as a static array or dynamic array. If we define them as a static array, this means that they cannot hold more than 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 the below ways.

Arrays in VBA Excel 2

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

  1. Static Arrays
  2. Dynamic Array
  3. One Dimensional Array
  4. Two Dimensional Array
  5. 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.

Popular Course in this category
Sale
VBA Training (3 Courses, 12+ Projects)
4.6 (247 ratings)
3 Courses | 12 Hands-on Projects | 43+ Hours | Full Lifetime Access | Certificate of Completion
View Course

Arrays in VBA Excel (Static Array)

#2 – Dynamic array

Array with a not predefined count of value that it can handle.

Arrays in VBA Excel Dynamic Array).

#3 – One-dimensional Array

An array that can hold only data either from rows or columns.

Arrays in VBA Excel (One dimensional array)

Arrays in VBA Excel (One dimensional array) 1

#4 – Two-dimensional Array

An array that can store a value from the rows and columns.

Arrays in VBA Excel (Two dimensional array)

Arrays in VBA Excel (Two dimensional array) 1

#5 – Multidimensional Array

Arrays in VBA Excel (Multidimensional Array)

Arrays in VBA Excel (Multidimensional Array) 1

How to use Arrays in VBA (with examples)?

You can download this Arrays in VBA Excel Template here – Arrays in VBA Excel Template

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.

ALT + F11

This will open the VBA editor. From there, we have to enter the code in “This worksheet.”

VBA Editor in Excel

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.”

Step 1

If we need a static array, then we will define a dimension as “Static.”

Step 1-1

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 the array can hold two rows value as excel counting starts from zero.

Step 2

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” mean that three columns.

Step 2-1

Here we have changed the rule of how excel counts rows and asked it to count from “1” and not from zero.

Example #3

The input of data in the array.

The data has to entered cells wise. Here the data is to be entered in the form of (I,j) where “I” means the row, and “J” means the column.

So “a (1,1”) means that cell ”A1.”

Step 3

Example #4

We are closing the code.

After the data has been entered for the array, the last step will be to close the code.

Step 4

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 the 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.

Recommended Articles

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 –

  • Top 4 Error VLOOKUP
  • VBA Macros Online Course
  • VBA Books
  • Excel VBA Arrays
0 Shares
Share
Tweet
Share
VBA Training (3 Courses, 12+ Projects)
  • 3 Courses
  • 12 Hands-on Projects
  • 43+ Hours
  • Full Lifetime Access
  • Certificate of Completion
LEARN MORE >>
Primary Sidebar
Footer
COMPANY
About
Reviews
Contact
Privacy
Terms of Service
RESOURCES
Blog
Free Courses
Free Tutorials
Investment Banking Tutorials
Financial Modeling Tutorials
Excel Tutorials
Accounting Tutorials
Financial Statement Analysis
COURSES
All Courses
Financial Analyst All in One Course
Investment Banking Course
Financial Modeling Course
Private Equity Course
Venture Capital Course
Excel All in One Course

Copyright © 2021. CFA Institute Does Not Endorse, Promote, Or Warrant The Accuracy Or Quality Of WallStreetMojo. CFA® And Chartered Financial Analyst® Are Registered Trademarks Owned By CFA Institute.
Return to top

WallStreetMojo

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

* Please provide your correct email id. Login details for this Free course will be emailed to you

Book Your One Instructor : One Learner Free Class
Let’s Get Started
Please select the batch
Saturday - Sunday 9 am IST to 5 pm IST
Saturday - Sunday 9 am IST to 5 pm IST

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

WallStreetMojo

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

* Please provide your correct email id. Login details for this Free course will be emailed to you

Login

Forgot Password?

WallStreetMojo

Download Arrays in VBA Excel Template

New Year Offer - VBA Training Course (6 courses, 35+ hours video) View More