Hey guys, welcome back. So in this video, we're going to discuss the on error go to zero statement. So the on error go to zero, what it does is that it resets the error handling status in your VBA code to the default status, which is basically throwing an error. So in case that you do not do any error handling, what's going to happen is that when VBA encounters an error, it's just going to throw the runtime error on the message box and you're going to see a message box that says, runtime error. This is the default error handling status in Excel VBA. And what this is useful for is if we have different statements that can potentially cause errors on our code, and we want to treat each statement differently or handle the error that could potentially be caused by each statement differently.
So for example here on this example, we've got the line of code worksheets my worksheet, select. This could potentially throw an error. However, if we run it right now, while the worksheet that is called my worksheet is created, it will not throw an error. And we've got the statement here on error go to create my worksheet. But what if I put another line of code that will create an error, so I put range a one equals one divided by zero, so this will create a division by zero error. Now watch what happens when we run this line of code.
So let's start running the macros step by step. So this line of code will not cause an error for activating the worksheet called my worksheet because it already exists. However, when we run this line of code, it will actually go to the create my worksheet, because remember, guys when I told you that when we write an on our statement We'll actually start a status of error handling for any statement that follows this statement. So as you can see here, if we run this line of code, and it will cause an error, it will actually go to create my worksheet and it will create a worksheet that is called my worksheet while we do actually have a worksheet already, so this will throw an error. So actually, this error was handled using the same way as the error that would potentially be occurring from this statement. And this is something that we do not want to do because this is division by zero error.
It's not related to creating a worksheet, so we would want to handle it differently. So if we add a statement here on error, go to zero. This statement resets the error handling status in your VBA code. So now we will go to the default error handling status which is actually that if we encounter an error Here we're going to have a division by zero error and then we can actually put another type of error handling so on our go to message box, for example, and then we would have here a message box placeholder here and we would have a message box, you cannot divide by zero. And it is better as well to put an on error go to zero statement here so that any statement after that one here would not be handled the same way unless you have more statements that can be handled the same way.
Or that can take the same error handling guys on our go to message box so you can put them here in between the on error go to message box on the on error go to zero. So as you can see here, we've used the honor go to zero statement here to reset the error handling status so as to go back to the default status and start at different status for handling this different error. So as you can see here, if we run the code right now worksheets my worksheet does so like so this will not cause an error and then we go to the default error handling status and we start a new error handling status which is to go to the message box. So now this statement causing an error will go to the message box you cannot divide by zero and then we will reset our handling status again and exit the sub so we use the honor go to zero statement to reset our error handling status because not all errors should be handled the same way.
So that's it guys for the on our go to zero statement. Thank you very much for watching this video and I'll see you on the next one.