So, the example I'm going to show you,

we're going to create a VBA array function.

Instead of counting the number of items that are divisible by n, what we're going to do,

is we're going to highlight a three by three and that corresponds to

the same size as the array, the argument.

So, we have a three by three output and then we're going to use divisible

by n and we're going to do Control-Shift-Enter array functions.

You need to Control-Shift-Enter to get the output,

to apply that formula,

that function to all of the cells in that array.

And the output is going to be zero,

if the corresponding number is not divisible by three,

in this case and it's going to be one if it is.

So, the four here is not divisible by three,

so the corresponding element is zero.

The nine here is divisible by three, so we get a one,

similarly a negative three and a negative six are both divisible by three.

I've got some data here.

This is a little bit different from what I just showed you in the diagram,

but negative nine is divisible by three, three is in six.

So, I want to be able to highlight three by

three and I want to be able to type in divisible by n,

and then, I would put in my range as the argument and divisible by three.

And I'll do Control- Shift-Enter and then it would give me a zero or one,

depending upon if the corresponding element is or is not divisible by three.

Now, this is known as an array function.

We're going to have to define or create

a separate array that's local to VBA in order to solve this.

So, if you're doing array functions almost always,

you have to create an array that's local to VBA.

Also for array functions is almost,

always necessary to do option base one,

so that we don't have a zero row in a zero column.

So, we're going to set this up similar to the previous example.

I've just called this DivisibleByN.

We're still going to have a range as the first argument.

We're going to have an n as an integer.

So, then what I'm going to do,

is in order for this to work, we have to,

it's no longer to be an integer because the output is not a single value, it's an array.

So, I'm going to Dim this as a variant.

I could also leave that off because variant is the default type.

We're not counting anymore,

so I can delete C. We're going to Dim i and j,

we still need to iterate through.

We're going to Dim, we're going to count the number rows, count the number columns.

We're going to enter into this double iteration.

Now, instead of counting the number of items that are divisible by n,

we're going to create a new array.

So, I'm going to create a new array B.

At the very beginning we don't know the size of B but this is going to be a variant.

So, this has to be a variant.

Instead of counting the number of items,

we're just going to replace B by default is going to be just a bunch of zeros.

And if the corresponding element of our range is divisible by n,

then the corresponding element of our B,

vector B ( i,

j) is equal to one.

All right. So, if range cells i,

j is divisible by n,

that means the remainder is zero.

Then, we want to have a corresponding element of one in our B array.

You also notice that I'm using option base one up here.

The output of the function is not going to be C,

it's not going to be the count.

It's just going to be B,

which is are our array of zeros and ones.

I did forget to re-Dim B.

After we count the number of rows and count the number of columns,

we need to re-Dim B.

Now, we know the size it's nr rows and nc columns.

So, we re-Dim that as a variant and I think we're ready to go.

So, let's go ahead put a break point here.

We go over here to the spreadsheet.

We highlight DivisibleByN.

The first argument is the range,

the second one is the integer that we're determining if those items are divisible by.

We don't just press Enter,

we do Control-Shift-Enter and we go back to the locals, window at the debugging.

We can step through this.

We count the number of rows.

We re-Dim B. So, you notice B now is a 3 by 3.

And we go through here and we step

through and you see that we have empty items and when we export that,

it'll just be zeros on the spreadsheet.

Otherwise, so our one,

three position that corresponds to negative nine

on the spreadsheet that is divisible by three,

so that's equal to a one and then we keep going.

If we open up the second row,

we see that there's this two,

two position is also divisible by three.

As we go through the last row,

we also set that last item to a one.

And then finally, we export that to the spreadsheet and we end the function.

And you see that the result then is an array.

So, this is an array function that provides an array as the output.

So, we have zeros where the corresponding elements are not divisible by

three and we have a one in the other elements of our array.

So, that's how you can make an array function in VBA,

a custom array function.

Now also, I didn't mention this,

but Excel has a bunch of built-in array functions.

We're not going to use them too much in this course,

but you can highlight a three by three array.

There's things like transposes.

So I could do that transpose of an array.

The transpose of an array just flips the rows and columns.

So, when I do type in transpose.

I do Control-Shift-Enter, it'll take the argument of

the function which was A2 to C4 and it just flips the rows.

It just kind of flips it about the diagonal elements here.

So, this becomes this two,

one position becomes the one, two position.

There's are some mmult.

So, you can multiply two arrays.

There's the matrix inverse.

There's a bunch of array functions that all require Control-Shift-Enter.

So, we can create, then,

I showed you in the screen cast how we can create

a user defined array function. Thanks for watching.