I've got another example here of an array formula, an array function. We want to create a VBA array function called diagonals that's going to extract the diagonal elements of a square matrix and place them in a column vector. This is inspired by some statistics stuff that I was doing for one of my classes. We created this something known as a covariance matrix. And this is a square matrix, but really all we needed were the diagonal elements of this array. And this can get really large, and you're just kind of manually plucking out these values on the diagonals. And so I created a quick VBA array function to do this. So when we do that then we can type in diagonals of the square matrix here. Ctrl+Shift+Enter, and it just plucks out those diagonal elements. So we can write in our function diagonals. We just have a single argument, we need to count the number of rows that are going to be in our output. The output vector is just going to be a column vector of size whatever the dimensions of the covariance matrix, or the range. So we can count that, we need to dim a few variables so I've dimmed i. We're going to be doing an iteration, we've got n that's going to be the size. And because this is a square matrix, I can either count the rows or the columns of rng, which is the argument of this function. So then I've set up a for loop. The diagonal elements are those where the row equals the column. I also need to dim up here my vector, the output vector that's going to be diagonals, and I'll call this D. So we need to dim D, we don't know the size yet. After we count the size and then I can read md, D is going to be an n by 1 cColumn vector so I've ReDimed that. Now, the elements of D are just going to be the ii components of a range array. So I'm going to define each element of D. Remember, D is going to be a column vector = to rng.Cells i,i. So we're going to click out the 1,1 element of the range. We're going to click out the 2,2, the 3,3, and so on. So whenever the rows equals the columns, that's what a diagonal element is of a square metrics. So we're going to do that, we're going to create vector D, it's going to be a column vector and then the last thing we need to do is output D. So the output of function is always the name of the function, and I think we're ready to go. So let's go ahead and put a breakpoint on there, I'm going to go over here to Excel. You have to know the size. So it's a 5x5 square matrix, so I have to put in a 5x1 vector area in Excel. So this is going to = to Diagonals of our array, here. And I do Xtrl+Shift, enter and go into debug mode. The first thing we do is count the number of rows, and that's going to be 5, so I can step through this using F8. So if we go i = 1, the first element of D is going to be = to Cells 1,1. And I forgot to do one thing. You noticed here D has a zeroth row, so I'm going to go ahead and stop this, and in this case, I need Option Base 1. So now with Option Base 1, it should work just fine, and I can step through this, n is 5. D now does not have a zeroth row, so we're all good. We can go through here, and we can open up the first, second, and so on. So we've plucked out the 36.5, that's a 1,1. We've plucked out the 2,2 position so that's 1.9 times 10 to the -2. It's in scientific notation over here and so on. So it looks like we're working so I'm just going to resume, and we end up with all the diagonal elements of our square covariance array. So that's just another simple example of how you can use array functions to do different things in Excel VBA.