This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code.
This lesson, Part 1, is the basics. Whether you are writing VBA for Excel, Access, Word, or another Microsoft Office application, basic error handling is the same.
01. Set up the error handler
At the top of the code for your procedure, the error handler is set up using On Error GoTo Proc_Err
02. Exit Code
After whatever your procedure does, a line label for the exit code (such as Proc_Exit: ) is used to signify what happens at the end of the procedure. This can be code to cleanup object variables, or simply code to gracefully exit.
03. Error Handling Code
After the exit code, a line label for the error handling code (such as Proc_Err: ) is used to begin what happens if there is an error.
Data can be kept in different media, Sometimes, the data need to be extracted, transformed or loaded in different ways.
For this article, I'm going to demonstrate an effective way to find the last line content of a large text file using VBA codes.