 back to another video. The topic for this video is looking at the if statement within the using Excel VBA. So we're going to go through a few examples of how to use the if statement and a couple of variations of using it, so obviously working with multiple pieces of criteria. We'll say without further introduction we'll just jump straight in. So what we need to do is as you're probably familiar with in all our previous videos in this VBA series, if you aren't, obviously you can go check them out in the playlist that is linked in the description for this video. Else, as a quick reference, all we're going to do is obviously code in this module, so to do that we just need to go insert and do module and then we can start typing in our sub routine. So the first thing we're going to do is a very simple if statement. So we need to do a sub, it's great obviously to store this as a sub routine and we're going to call this let's call this if statement or go if example. Okay, so in order to work with if statements we're basically going to use obviously variables and obviously then pass those through the if statement or if function to obviously determine what course of action we're going to take based on that variable or that comparison that we've performed. So the first thing we're going to do is create another create an initial variable called number one. So this is where we're going to store a number and then we can use or do some comparisons against this number with our if statement. So the first thing we're going to do and again this is just for best practice you don't need to set variables as we've discussed previously but we're just going to stick with best practices and define our value as our desired integer value. So we're going to do dim number one as integer and then we can now move into our if statement. Honestly before we do that we've obviously got to find what this value is. So we're going to do number one equals 10. So very simple there's just a straightforward number. So the first thing we want to do is with our if statement as as we'll be doing our examples is taking this number one and just passing it through a few pieces of criteria. So the first thing we're going to do is see or test if the number we've got or number one which is the value of 10 we're going to see if that value is greater than five. So the first thing we need to do is go if number one because that's our variable is greater than so if you're not familiar with these symbols I'll go over them as we go through. So the greater than symbol is what I just used there and to get that on the keyboard you do hold down the shift button and you hit the full stop button on your keyboard. So greater than is you can see they've got the larger side on the left and the small on the right so greater than if you wanted to do less than it's just the opposite that so you do shift and the comma button so you can see we've got the less than icon there. So sort of digress a bit there I'm going to go into these again in a minute but there you go just cover it off just so before you got stumped with what those symbols are. So we want to test to see if our number was greater than five so if we go is if number one is greater than five then and now we can move into what logic we want to happen if this is true. I'm just going to do an extra space you don't need to a new line shall I say you don't need to but I hope this is probably makes it a bit clearer on the screen for your viewing and I was going to tab in just so we can keep our code clear again. So if number one is greater than five we want to present a message box that says um number greater than so simple as that if our message box or if if our if number one is greater than five then we want to display a message box that says number greater than five. I'm just going to actually tuck this in a bit more because it does look a bit better actually and that is the only logic or criteria we're going to apply at this moment so therefore we now just need to end our if statement so we just do end if so that is the entire if statement we need to test if our value is greater than five if we now run this so we can either do that by our apply button or hitting f5 you can see we get a pop-up that comes up and says number greater than five so it's all worked as we're completely intended if however we go okay if we wanted to test this and we changed our number to four so obviously a number that is less than five you can see when we now run the code nothing is happening because we haven't defined anything we want to happen so what's happening at the moment is it's going into our code it says if number one is greater than five well four is not greater than five so all it's thing going to do is exit this if function and end so there's actually no criteria to happen in order to do that we now need to enter another piece to our if function so the first the most basic option for us to do is we can say else so simply type the word else new line and you can see as we're all rvba code you can tell that you're you'll type the word correctly when the reformatting happens like this and you can see it's actually turned the color blue as well so first part if number one is greater than five then obviously we want to display a message box of number greater than five so here we can say okay well if that's not the case and number one is actually less than five we just now want to show a message box saying number less than five so we can see we've now covered off both options so if it's greater than five we're going to get the message saying number is greater than five if it's less than five then we're going to get the message boxes message box it says it's less than five so let's run this code now just check that's all working so yet we can now see that it says number is less than five because the number is four if we do change our number to the number six and run this again you can see it's given us the correct one again of number is greater than five so both of those scenarios are working the only problem we now have with this code is if we were to put the same number so if we to change number one to number five and obviously number five is equal to obviously the number five we're comparing against and if we were to run this you can see that we've actually got something was not quite right so you can see the moment because that actually the same our logic is saying if number one is greater than five obviously say number greater than five and else so it's going to just disregard what the number is it's just automatically going to put less than five if it's not greater than five so our logic is working in some respects but obviously it's not technically correct we actually need to put some or make a change to this so that we can actually say okay well it's we wanted to say the number is the same so the number is five in order to do that we're going to do okay so we can go into here and we can now say so let's go into here and go okay so we want to add some other logic so we can now put into here else if so else if is obviously a bit more detailed to else so when you work with the else part so we've got here and ignore that error just now popped up else is there's going to be no logic or anything applied it's literally going to say if nothing else matches your criteria or none of your other if statements obviously are a match it's just automatically going to dump or resort to this final line of code here but if we actually want to say well we want to apply some logic so if it's not greater than five but it is this then this is where we use else if so let's go into a new line here so else if and obviously got right on the same line here else if number one equals five then and now we can add some logic here so we can put message box number is equal to five so you can see we've now got three scenarios covered and let's just add some spaces so the first part I hope this is this is not mumbled together and it is making good sense and but hopefully if I talk through this now it starts bringing it together so the first thing it's going to happen is it's going to perform this if part and say okay if your number one is greater than five then perform this piece of code and obviously ignore everything else it's going to just jump all over this it's only going to execute this message box here so it'll say number is greater than five if that isn't a match because the number is actually less than five or it's it's not a number greater than five so it's not six or above then what it will do let's do this test it'll say else if number one is equal to five then present a message box that says number is equal to five if neither of those two pieces of criteria are correct so it's not greater than five and it's not equal to five we can quite confidently say well actually the number is then going to be less than five therefore all we need to do is spit out this message box that says message box number is less than five so if we now run this we can see yet number is equal to five it is indeed if we to do a number greater than five like six number is greater than five and if it to do a number less than we do four then we can execute our code and we can see number is less than five so all our scenarios are now covered off by using that else if statement with else if you can have as many as you require obviously you can have maybe one or you can have multiple depending on the data that you are working with so you're not limited to just just one else if you could have more on here if you wanted so as a really random example let's just go into here and say I was literally copying the code just to be cheating and be a bit lazy else if number one equals equals five then message or equals three sorry then message box number is equal to three so again if I was to run this code obviously at the moment number is less than five because we've got four but if we actually selected the number three and do that you can see number is equal to three so again the logic I just now applied here by entering in that is not quite correct because we've got obviously this less than five if it is less than five or if it's not three but you can just see that you have the flexibility of working with more than just the one else if statement if you require in addition to working with the symbols that we looked at here with the greater than we are also able to do greater than or equal to a number so if I was just to go into a maybe a new sub sub here so let's go sub um next random sub routine name but there is some logic in that and we're going to do number one equals to five if number one is greater than or equal to five then message box greater than or equal to five else less than five would be our message box msg box like that and if so a really quick example I've just done here and we can just remove all of this now so a new sub routine I've done here so our number one our number is equal to five and as with our previous example we use the else is statement to you know cover up scenarios where the number was actually less than equal to or greater than what we can actually do is tick off that by using a combination of these two symbols so we've got the greater than here and also combining it with the equal sign so that we can capture everything that is five and above rather than just greater than five so first to run this now you can see that our message box is going to say greater than or equal to five and that's because obviously we've now put this additional logic in if I was to put the number six you can see that if we were to run this again it's still greater than or equal to five if I was to do the number four and run that you can see it's going to automatically default to our else which is just it's going to be less so if it's not greater than equal to five it must be less than five hence we can just do this simple logic of else so that's another quick touch on another ability we are able to do with using combining the greater than or less than symbol with the equal sign as well and if you want to do that you just need to do that in this order so the greater than part first followed by the equal sign together to give you that logic so I hope you enjoyed that video and I gave you insight into using the if function within xlvba if you did enjoy the video please do give it a like as not only greatly appreciated by myself but it also does help that all important youtube algorithm if it's the first time checking out the channel or you've watched our videos before please do subscribe to the channel and also make sure you hit that bell notification button that way you'll be notified of all of our new videos as they come out and if you have any questions at all please just drop me a comment below this video and I'll get back to you as soon as I can so thank you once again and we'll see you in the next video