Excel VBA - Error Handling

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we'll see how to face the errors which we get into the macros that is Excel VBA sometimes you get an errors what are the actions you should take when the errors are occurred there are multiple ways you can handle the errors also known as error handling let us see that now I've written some code which has some errors some bugs and you have to see and observe find out what is the error in that let us see that now I'm in the file excel vba miscellaneous go to Developer tab Visual Basic on error resume next I have created a module on error resume next and that's a code very very simple code I have created as a sub procedure using resume next if you want you can change it I have written three message box message box ten message box 10/0 message box twenty very simple code now if you observe ten second one then divide by zero actually any number cannot be divided by zero okay so it should throw an error message okay so if I execute it you can observe here if I execute it step by step like this step into step into step into so I've got the answer is ten which is fine okay now if I execute the second statement click on just step into I'll get us runtime error 11 and the name error is nothing but divided by zero so number cannot be divided by zero and I can say as debug so it has come to that point and it has highlighted again if I click on step into it will not proceed further it cannot leave this statement and proceed further so suppose there are thousand lines of code and there is one line which has some error what can you do should you stop for every error so what can be done is instead of stopping for every error message for everything you can try to handle that error how that if there is an error in the second line skip that if the error is in the second line skip that and go to the next line and execute that particular sentence so what should be done to avoid that so there is a keyword which you can write down as on error resume next that means whenever you find out the error message on error you have to resume being that particular step and you can go to the next step now let us see that if I click on the step in to step into step into step into I have got a Sten again if I click on step into it did not show that message box errors runtime that is cannot divide by zero but it went to the next place and it's displaying and if I see I have got as twenty so remember on error resume next that means do not stop at particular location directly go to the next step itself that is one method right and I will say a step in so it came out of this sub procedure this is one method second method is if you want to notify to the user there was some error message at this particular location so how do you do that so for that you have to see us on error go to some location you cannot say resume next so what I can do is I can remove this area and LCS and go to ABC I am purposely giving ABC here so it's easy to understand and I'll write here as done and I'll say s exit sub okay if it is properly working all these places you have to say as done exits up if it is not working if there is some error message on error go to this particular label this is known as label and the label name is given as ABC so what I can do is I can draw here I'll can write here a label ABC and I can display a message box you can just write here something error message is XYZ I can write here anything so what I'll do is on error go to exit so if you know this on second step that is message box 10/0 I'll definitely get an error so if there's an error it will come to this ABC message box error message is X Y Z and then it will end so right so let us see if I try to execute this yeah if I click on step into step into step into the first one should give me as message box 10 which is working fine now the second message box then divide by 0 again if I say step into it didn't go to that particular place ok on error go to ABC it came to this ABC because 10/0 was giving an error message it came to this place and now if I step into error messages XYZ if you want you can just customize and put your error message and finally it has given us end sub procedure step out right so these are the different ways by which you can handle first one is on error resume next and second one is on error go to that particular label and the label can contain some functions some messages anything it can be done multiple things at this please I hope you have understood that's all for this video
Info
Channel: Tutorials Point (India) Ltd.
Views: 76,167
Rating: 4.8766861 out of 5
Keywords: VBA, Marcos, Microsoft, Excel VBA, Excel VBA - Error Handling, Error Handling
Id: gn37Awyxxt4
Channel Id: undefined
Length: 4min 57sec (297 seconds)
Published: Sun Jan 14 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.