Arrays Workout
- 10:28
A workout to demonstrate how a two-dimensional array can be used to perform calculations.
Transcript
We are required to write VBA code to declare a two-dimensional array Dimension.
One should be from 1 to 5 and dimension two should be from 1 to 3.
We then need to write four next looping procedure to assign the values in the range area.
Here we can see the range area to column one of the array in column two of the array, the values should be equal to the values from column one of the array multiplied by two, and the values in column three of the array should be equal to the values from column two of the array divided by 10, the value in cell B 15.
We finally need to sum column three of the array and assign this value to cell B 23.
Let's go into the visual basic editor and write our code.
We're going to write our code in a new module.
We insert module and we'll call our procedure workout 2.
The first thing that we need to do is to declare an array.
This will be a dimensional array with space for five elements in each column and three columns.
All of the data will be data type double.
We use DIM to declare the array and we'll call our array Test Array2.
The first dimension of the array should take elements from 1 to 5 and the second dimension of the array.
We're told to take elements from 1 to 3.
The data that will be held in that array will all be of type double.
This is the most flexible type as it can handle both whole numbers and decimals.
We now need a count variable that will be used to count through each of the items in the array and assign them with a value.
Let's call our count variable I and we can declare this as an integer type.
We now need to write our Four next code to fill each of those items within our array.
For I, our counter variable equals 1 to 5 each of the rows in the first dimension of our array, and we need to fill our first row of the array, the first row and the first column with the cells from the range area in the front end of Excel.
In order to do that, we identify our array called test array two and we identify a specific element within that array.
We identify the first row where I is equal to one and the first column.
This is the first element in our two dimensional array and we want that to be equal to the range area and specifically cells.
I where I equals one, one, and one.
We want the first element in our array to be equal to row one column one of that area, range of cells in the front end of Excel.
Let's deal with how we fill up the second column.
We identify that second column in our array named test array two row one, column two, and we want that to be equal to the value in test array.
Two row one column one, multiply by two as the instructions in our workout specified the final column of the array.
Well, we want that to equal the second column of the array, the value in that second column divided by 10, and we have the number 10 in cell B 15 in the front end of Excel test array.
Two, our newly created array.
Let's specify the elements within that array.
We want the first row and the third column, and we wish that to be equal to test array two First column Where I equals one first row rather where I equals one second column, but this time divided by range B 15.
The value property of B 15 is 10 and we can write looping procedure to go through each one of the rows in our array.
So when we move on to the next I, I will then be 2 and we fill all of row two in our array.
When I is 3, we fill row three and so on.
Let's adopt some good coding procedure and indent all of those instructions in the loop.
After all the elements of the array have been filled, the worksheet function index can be used to specify which elements we want to sum.
The first argument specifies the array.
The second is the first dimension here, zero as we wish to sum all of the rows in our array.
And the third argument, we specify the second dimension, and here we want to sum down column three of our array.
Let's write our function in range B 23.
We wish to sum all of the elements in our array and we need to use the worksheet function some.
To do that, we can use the worksheet function index to specify which cells in the array we wish to sum the array we identify as test array.
Two Second argument, we enter zero as we wish to rather some all of the rows in our chosen column.
And our chosen column is number three.
That is the third column from our second dimension of the array.
So worksheet function index is telling us to identify the test array two, all of the rows in that third column of the second dimension.
Let's close our brackets and return to the front end of Excel.
In the front end of Excel, we wish to execute our code.
We wish to execute our sub procedure named Workout2.
Now what we'll notice is that array does not have to be created in the front end of Excel.
These are all spaces in the memory of Excel VBA.
All of the calculations are being done in Excel VBA and we hope to get an output in the front end here in cell B 23.
Let's execute our code developer view macros and we're going to run workout2.
We get the figure of 700.
That figure of 700 of course is remember the sum of all of the elements in that third column, the column being the second dimension of our array.
That third column of the array is equal to the second column in our array, which is equal to the first column in our array multiplied by two.
And then we divide all of those numbers to get the third column figure.
We divide them by 10.
Let's test that this figure is correct. Let's divide.
Let's first multiply each of these elements by two and then divide by 10.
Copy all the way down, and if we sum each of these cells, we should get 700.