An Interactive Learning Tool

Proud to be a Teacher


Arrays

While working with one item a singal variable being defined, but for working with a list of items then we need to define an array or array of variables instead of using a long list of different variables. For example you are working with a long list of products and products need to be defined in variables, it will be very difficult to have multiple variables instead we will define and array for same group of data type and name. Declaring Arrays in Excel VBA
We use the Dim statement to declare an array just as the way we declare a single variable.In Excel VBA, we can have a one dimensional array, two dimensional array or even a multidimensional array (up to 60)

One Dimensional Array

The statements to declare a one dimensional array in Excel VBA is as follows:
Dim arrayName(index) as dataType or
Dim arrayName(first index to last index) as dataType

For example,
Dim StudentName(10) as String
Dim StudentName(1 to 10) as String
Dim StudentMark(10) as Single
Dim StudentMark( 1 to 10) as Single

Example 1

In this example, we define an array StudentName of five strings using the Dim keyword. We include an InputBox to accept input from the user. We also use the For ...Next loop to accept the input five times and display the five names from cell A1 to cell E1. The code is as follows:
When we run the program, an input box will appear, as shown below. This input box will repeat five times and let the user enter five names.
The five names will be displayed in the spreadsheet as shown below:

Example 2

You can also declare more than one array in a single line. In this example, we declare three arrays in a single line, separated by commas.
When we run the program, three input boxes will appear consecutively to let the user enter the student name, the student ID and then the student mark. The process will repeat three times until the particulars of all three students have been entered. The three input boxes and the output are shown below:







Two Dimensional Array

Multidimensional arrays are often needed when we are dealing with more complex database, especially those that handle large amount of data. Data are usually organized and arranged in table form, this is where the multidimensional arrays come into play. However, in this tutorial, we are dealing only with the two dimensional array. Two dimensional array can be represented by a table that contains rows and columns, where one index represents the rows and the other index represent the columns. The format to declare a two dimensional array is
Dim arrayName (num1, num2) as datatype
Where num1 is the suffix of the first dimension of the last element and num2 is the suffix of the second dimension of the last element in the array. The suffixes of the element in the array will start with (0, 0) unless you set the Option Base to 1. In the case when the Option Base is set to 1, then the suffixes of the element in the array will start with (1, 1). For example,
    Dim Score (3, 3) as Integer
will create a two dimension array consists of 16 elements. These elements can be organized in a table form as shown in the table below:


If you set the option base to 1, then there will be only 9 elements, i.e from Score(1,1) to Score(3,3). However, if you want the first element to start with suffixes (1,1) you can also use the following format of declaration:
    Dim Score(1 to 3, 1 to 3) as Integer
If a company wants to track the performance of 5 salespersons over a period of 2 days, you can create a 5x2 array in Excel VBA, denoted by a 5X 2 table in a spreadsheet.
You can write the following VBA code:


When the program is run, the inputbox that will prompt the user to enter sales volume will appear 10 times, as shown below:

After all the sales Volumes are entered, the values in the spreadsheet are shown below:


If you need to make sure the user enters the correct sales volume, you can change line 5 statement to
SalesVolume(SalesPerson, Day) = InputBox("Enter Sales Volume of " & " SalesPerson " & (SalesPerson - 1) & " Day " & (Day - 1))
Clearer instruction will be shown as follows:




Terms of use Website Policy Contact us
© Copyrights ExcelYourVBA.com 2020