Alright guys, so let's have some examples for the different types of errors that we might encounter in Excel VBA. So if you right workbooks, my workbook, and then I missed the second double quote here and close the brackets dot open, you can see here we got a compiler. But before we even got the message box for the compiler, you can see here that our code turned red. So this is a syntax error. Your code turning red means you have a syntax error, you have a grammar or spelling mistake. So our grammar or spelling mistake here is that we didn't put the second double quote.
So this is a syntax error. Now to have an example for a compiler. Let's say we put instead of workbooks, we put just the word workbook here. Now, this is still not generated this syntax error because the word workbook is a word that is part of the Excel VBA dictionary. So VBA recognizes this word as part of its dictionary. And so it will not tell you there's a spelling mistake.
However, when it tries to run the code, it will generate a compiler because although the word workbook exists in the VBA dictionary, it is not put in the right context, it's it does not make sense to put it in this context. So if you run the code here, you'll get a compile error because the word workbook cannot be used in this context. So you need to use workbooks, not workbook. So this is a compiler. Now to have an example, for a runtime error, if we write worksheets, my worksheet dot select, as you can see here, our code has not turned red. So that means that there isn't a syntax error.
And also, we can check for a compile error. And we can do that even before running our code by going to the Debug menu here and clicking on compile VBA project. So if you click on that, And we don't get an error. That means that there aren't any compile errors on our code. And if we run this code, though, we will get an error. And the reason for that is that although this action is a valid action to select a worksheet called my worksheet, we will not be able to do it.
Because we do not have a worksheet that is called my worksheet in this current workbook. So if you run that, you will get an error subscript out of range. And that is because we do not have a worksheet in this workbook that is called my worksheet that we can select. So this is about action. However, we cannot do it because there isn't a worksheet that is called my worksheet that exists. Now the errors that you're going to be dealing with the most or that you're going to be encountering the most are actually runtime errors.
Because syntax errors are going to be pretty obvious when you write your code you will have it highlighted in red indicating the part of your code that is having a problem and you Most of the time, you'll be able to figure out what the problem is. and compile errors as well are going to get you an error on that part of the code. And actually, your code will not start running in the first place. So be able to discover it pretty early. Over runtime errors can happen actually, after the code runs for some time. And even after you try the code several times, where on some trials, your code runs fine, and on others, your code would generate an error.
So this is actually the error type that you're gonna be spending most of your time focusing on and handling, which is the runtime errors. So on the upcoming videos, we're going to be focusing on how to handle runtime errors. We're going to see different methods and techniques to do that. So thank you for watching this video and I'll see you on the next one.