Okay. In a previous screencast we had created this guessing game using a do loop. So I wanted to show you what the result of that was. We click on the guessing game, it's thinking of a number between one and ten. We can enter a guess, and we keep going until we get the correct answer. However there's a problem because if the user puts in a string, so just put in some numbers and you click OK, it brings up the editor window with this error window here, and this is very unprofessional and you don't want this to be happening. Also, if you just press the cancel button it does the same thing. And if you press the OK button when nothing's entered and also when you press that close button the same thing happens. So what I'm going to show you in this screencast, there's two parts, it's quite long. This is all optional, and it's advanced, it's okay if you don't understand everything. But if you're interested, go ahead and watch these two screencast. So I've got a guessing game here, and now when I type in letters it says that your guess must be a number, so it's doing some advanced input validation and error handling. I can also put in negative numbers, the guess should only be between one and 10, so it'll tell the user. For example if I input something greater than ten, guess must not exceed 10. And also when I press cancel it just closes. And when I press the close box up here it just closes. So that's we're going to be making in this pair of screencast. Okay. I've got a really advanced problem here. Don't worry if you don't understand everything I'm going to talk about here this is sort of more for your benefit. I already had a guessing game sub, where the program chooses a random number n, we then ask the user for a guess between one and 10, and it keeps going until the user guesses n. The flowchart for that is shown here, just by noticing the space available on this flow chart all the wide open space you can imagine we're going to make some improvements to this. But this is the flowchart we start, we choose n, we get G, that's the guess from the user. If G equals the random number that was chosen by the program, then we message box something like congrats. Otherwise, we go back up and we get a new guess. The first improvement we're going to make, that you guys have learned about is how to make sure that G is between one and 10 inclusive. So shown here, after we get G, we want to make sure both of the conditions G greater than equal to 0 and G less than or equal to 10 have to be true. And if that's the case then we move on and we do the game, the guessing game. If at least one of these conditions is false then we're going to go in and we're going to check which one is it, is G greater than 10? If that's true, then we're going to message box maybe something like, invalid entry your guess is greater than 10. Otherwise if it's false, then we check to see if G is less than zero. If that's true then we have a message box, something like, your guess was less than zero. Please try again. In either case, we go back up to the loop, we ask the user for another guess. What we're doing here is making a major modification this entire rest of the modification to you're all going to be made to this line. So we have this outer do loop which is the main structure of the game. Then we have this inner do loop here, which is all the input validation that we're going to be doing. Inside the inner do loop, we get G the input from the user, and then we ask ourselves is G less than or equal to 10 and greater than equal to 0. If that's the case then we exit the do and we move on to the rest of the game. So if we don't satisfy both of those conditions, then we check to see if G is greater than ten. If that's the case we're going to message box, something like, guess must not exceed 10. Please try again. Otherwise if G is less than one, we're going to message box, something like the guess must not be less than one. Please try again. And we move along. So let's look at the performance of this. Let's see how it's working. We just protected it for numbers that are less than or equal to 0 and greater than 10. So let's run through this. If I put in something like negative nine, and we run through that, you see that it bumps down into this, and it says the guess must not be less than one. Let's reset this and run it when, let's check to make sure that greater than 10, 19, big bumps in there and says guess must not exceed ten. Try again. There's a problem however, now if the user does something that doesn't make sense, like they put in pi, or they just put in letters, it brings up this error box. And if you don't have the editor open you're just working in Excel. It'll actually open up the editor, which is very unprofessional, it's intimidating to the user. Especially if they don't know anything about programming in VBA. So you want to try to minimize that as best as possible. Another thing, sometimes when you just input box pops up and you just press something like cancel, it does the same thing. Where it goes into this warning. One way to deal with this, it's sort of just a catch all. If there's any sort of errors that occur, you can use this on error go to statement. So I've set that up. I've put this on error go to here at the very top, underneath my dim statement. If there's any sort of error in the code it's going to bump to a bookmark, basically a bookmark that you define called here. So it's going to bump down to the bottom here. I have here with a colon. And after that, I put a message box, an error has occurred. Now importantly, if there isn't an error it's going to go through here fine, it's going to output this message box with the congratulations. And if you don't have an exit sub statement here, it'll tell them that there's an error anyway. So this exit sub, just kind of bypasses that message box. So now when we run this, I can put in something that doesn't make sense like a string or something. And you see that it bumps down, and it says an error has occurred. Now it doesn't, it's not more specific than that it just tells them, that an error has occurred. If I just click the OK button without any sort of stuff in there it's going to do the same thing, it bounces down and tells him that there's an error. So what I've shown here is fine for many applications. Now if you want to really delve deeper into identifying the different things, for example letting them know that they didn't put anything into the input box, letting them know that they canceled, and so on. Then we can make improvements to this and so that's what we're going to do next. The next improvement we're going to make is if they enter an integer. So, here. You notice if I put in an integer like seven point five, that if I open up the locals window down here. It actually converts that around to an integer, so around at seven point five up to an eight. But maybe we want to let them know that they have to enter an integer, it can't be a non-integer, so let's make improvements to this. The first thing we do is now we have to have three things satisfied, G has to be greater than or equal to 0, less than or equal to 10 and a non-integer, so I added one little line there. All three of those have to be true in order for us to exit and continue on with the guessing game. I've added in this other test. So we want to identify which of the three of these was not true. So we test to see if, G is greater than 10, less than 0, and now we see if it's a non-integer. If it's a non-integer, then we're going to message something like your number is a non-integer. And then all these come back together and we go back up to complete the loop. First of all, in order for us to differentiate between an integer and a non-integer, I have to change the data type of G to a double, next. Now we've got here in the flow chart three conditions that have to be true. We've added on the non-integer. So I'm going to add in onto this line, we have to have three things that are satisfied. And I'm adding in, there's this you can take the integer part of a number, and then we're going to convert it to a string because, whatever is input into a input box is treated as a string. So we're going to see if the integer part of G is equal to G itself. This is only going to be true if G is a integer. I've added in one more LCF, to see if G is a non-integer. So we can identify the error that's involved. And we have a message box, guess must be an integer. So the code worked just fine before we did all this, but it just didn't identify that the user was inputting a non-integer. So let's go ahead and just let me show you what happens when we run through this. If the user inputs something like seven point five, then, that's all three of these are not satisfied now because the integer part is not equal to the G itself. So we bump down, and this is identified as being a non-integer. So then it outputs guess must be an integer. Please try again.