Page 110 - Microsoft Office Excel 2003 Programming Inside Out
P. 110

Microsoft Office Excel 2003 Programming Inside Out

                             If your error handler is able to fix the problem that raised the error to begin with, you will
                             want to resume execution of your procedure. That is done by using the Resume command.
                             Placed inside of an error handler, it resumes execution with the statement that generated the
                             error originally.

                             At times, the error handler might not be able to correct the problem, but if the error itself was
                             not fatal to the execution, the error would not cause the rest of the procedure to fail. If that’s
                             the case, you can resume execution of the procedure by using the Resume Next statement.
                             This continues execution with the statement immediately after the statement that raised the
                             error. You can also specify execution to resume at a specific statement by using the Resume
                             line statement. This will continue execution at the statement named by line.
                             The following code fragment illustrates a common method of using error handlers:

                             On Error Goto ErrorHandler
                                 [statements]
                             FinishedSub:
                             Exit Sub
                             ErrorHandler:
                             MsgBox(str$(Err) & ": " & Err.Description, vbInformation, "Error Handler")
                             Resume FinishedSub

                             This rather lengthy, though hopefully complete, chapter has covered all of the basic skills you
                             need to begin creating VBA macros. You’ve learned how to define and assign values to vari­
                             ables, to control the flow of your programs using loops and tests, and to handle any errors
                             that come along. In Chapter 5, you’ll learn how to create and use procedures that contain
                             your VBA code.



             Chapter 4


























                84
             Part 2:  Visual Basic for Applications
   105   106   107   108   109   110   111   112   113   114   115