 back to another video. So the topic for today's video is to introduce you into using loops. So loops gives the ability to pretty much, as it says, is to define a range or a number of rows or columns and to loop through each one of those cells within that range and perform a calculation or obviously insert a value. As this is the first introduction, this video might be short of the normal and obviously might be quite basic in its actual output. But I wanted to start this next look at loops, obviously with something really basic to help you get the grips of it before we move into more advanced uses of such functionality. So the first thing we're going to do, well, not the first thing, but what we'll be doing is using our loop to automatically populate the values within column A for a defined number of rows. And the code is really simple. It's very straightforward on how to do this. So it's going to be a great one for you to learn and have under your, or in your pocket, so we say. So the first thing we're going to do again, we've got our module here ready to go and we're just going to enter our subroutine straight away. So our sub, we're going to call this, we'll calculate the capitals, sub equals simple loop. Now do that and it will come down. So the first thing we need to do is we're going to define a variable for the letter i and that's going to be an integer and this integer or variable is going to then refer to the row number of our Excel worksheet. So we'll do dim at i as integer. And it's nice and easy to remember i for integer and obviously that's a number. And then the next thing we're going to do is saying for i equals one, two, six. So what this means is our value of i is going to start at the number one and it's going to iterate all the way up until it reaches this value of six. So before I go any more than that, I'm going to enter the rest of the code and then we can talk through it and hopefully that'll make more sense. On a new line, I'm just now going to define what code is going to happen within this loop. So we're going to refer to sheet or sheet, so I say. And that is going to be sheet number one and you can see that's just the sheet number that we're using here today, closed brackets. And I'm going to refer to our cell range or cell reference. So we're going to use cells, open our bracket. So the first thing, well, when we're using cells, obviously we have to put in these two pieces of information. The first part is going to be the row reference. So say if we wanted to refer to cell A1, it's going to be the first row within the first column of our sheet. So that's what we refer to to cell A1. If we wanted to refer to cell B1, obviously it's still the first row in our sheet but it's now going to be the second column. And likewise, if we now wanted to refer to row two of column A, we enter in here row two of the first column closed brackets and that's referring to that cell. Because we're going to be using this loop and it's going to be dynamically going through each of these numbers. So starting at one or row one, you could say, all the way to six, we're actually going to be entering our variable into the row reference. So we've got I there comma and obviously we want to be in this first column or column A. So it's going to enter the number one closed brackets dot value and we'll just say that equals to the value of 100, the number 100 on the new row. I'm then going to just enter next I and then this is all the entirety of our code we're going to be using. So the first part we've got here is we've defined I, the letter I as an integer. So a whole number. And then what's going to happen is for I equals one to six. So it's going to start at the number one. So as it starts its existence, when it does this loop, I equals one. It'll then run this code. So it'll say, okay, for this sheets and cells I one. So it'll look and say, okay, well I is currently equal to the number one. So it'll be the same as having the number one here, which is going to be this first cell. And what it's going to do once it's identified that cell, it's going to change the value to 100. So it'll enter the 100 here. Once it's done that code, it'll then go to next I. And in essence, in the most simplest terms what's happening, it's actually now going to add one to our existing I. So one I, which was one, is now going to become two. And then what will happen is the sheet, the code will then execute again. So sheets, sheet one, cells and I is now equal to two. So it'll now reference our second row within that first column and the value be done to 100. This will keep performing all the way until it gets to row six because we only want to go up to the number six. So soon as I is equal to six, it will perform this code, but there is no I thereafter. So there's no next I because we've reached the maximum and then it will exit the code. So I think the only thing for me to do is to run this code and hopefully that maybe sort of a bit mumbled explanation makes sense and you can see how it works. So let's just change this here and then move it six. So if I now run this code, so as you can see, it's the code is executed and it's run through each of those rows within our range. So it's obviously going up to number six. So you can see our sixth row and entered the value 100. And I just now thought of a simpler way of demonstrating this. So I'm just gonna clear this and what I'm gonna do is add a break to this next I part of our code. And you do that simply by clicking just to the left there on the side. You can see it's been highlighted with this circle to the left. And what this is gonna allow us to do is all the code will execute until it gets to this pause line and then it will momentarily pause so we can see what's happened within the code. So if I was just to run this, so I'll do F5. You can see what's happened is currently to start with I is equal to one. So it's gonna replace I within our cell reference here. As you can see, I hover over I is currently equal to one. Therefore it's gonna refer to the first cell within the first row of our sheet and enter the value of 100. If I now hit F5 again, you can see our code will continue. But this time I, if I can hover over it correctly as I did before. Yeah, there you go. I is now equal to two. So what it's gonna do is refer to the second row within the first column and then turn make that value equal to 100. And so on as we go through this, so you can see it's now gone, I is now equal to three as it's passed through that next I aspect. Once again, you can see I is now four. So it's therefore referring to the fourth row. One more time has done the fifth and lastly it's now gone to six. And this time if I now exit code once again, you can see that it's actually exited the code because there's no more code to go through because we've reached our maximum of six, therefore it's exited the function. And this code is really beneficial and helpful when we're sort of going through and not even comparing or wanting to perform calculations or references against a lot of data. All we need to simply do if you want to go further than six is you can change this number to 10, hit F5 and you can see it'll go down to the value of 10. Alternatively, if you wanted to change this to 150, again, we can run this code and it will automatically go through each row in our reference and populate that value to 150 as well. So again, hopefully that video was seemed quite straightforward and logical and has given you a basis to actually work on and give yourself something to try and play around with. As we go further and I know I keep saying this in videos but we will be getting onto these very soon. I assure you is when we get into more complicated and more sort of technical logic, you'll really see the benefit of using these loops to work with. Also another benefit of, again, keeping your interest with using the loops is once you're working with a lot of data and I'm talking like a large number of rows of data, if you're performing normal Excel calculations, obviously that can have quite a burden on your file and take up quite a lot of memory and time to perform those calculations. Whereas if you're using your calculation in VBE or VBA and you combine that with a loop to perform for each applicable row, obviously that is a real sort of lifesaver in some senses and a lot quicker for performing the complex calculations as well. So I sort of digress there a bit but I was trying to give you a bit more insight into the real sort of benefit and the uses that these loop functions will have later in your Excel coding. So hopefully you enjoyed that video. But if you did, as always, please do give the video a like. If it's your first time or you've watched our videos before and you've not already subscribed to that channel, please do hit that subscribe button and also hit that bell notification button as well so you're notified of all of our videos as they come out. As always, any questions do, please just drop a comment below this video and I'll get back to you as soon as I can. So thank you very much for watching and we'll see you in the next video.