Module 7 - Error Handling
Lesson 7.1 - The On Error Statement
Topic 7.1.2 - Disabling an Error Handler

In the previous part of the lesson you saw how to enable an error handler in a simple procedure using the On Error statement.  In a more complex procedure you may wish to enable an error handler for only a small part of it.  This part of the lesson explains how you can disable an error handler after it has served its purpose.

Files Needed

You can click here to download the file for this part of the lesson.

Completed Code

You can click here to download a file containing the sample code.

The Example Workbook

Extract and open the workbook linked to in the Files Needed section above.  You'll find a workbook which allows you to create a list of hit and flop movies released in a particular year:

Example

You can select a year from the drop down list on the Menu sheet. Clicking the Get Hit Results button should create a new worksheet called Hit Results and copy all of the films made in the selected year from the Hits worksheet.

 

Identifying the Problem

The subroutines attached to each button on the Menu sheet will generate a run-time error when attempt to run them.  Try clicking the Get Hit Results button:

Run-time error

You'll see this error message when you click the Get Hit Results button.

 

Click Debug on the dialog box shown above to identify the line which has caused the error:

Error line

The line highlighted in yellow attempts to delete an existing worksheet called Hit Results.

 

The line highlighted in yellow above fails because the Hit Results worksheet doesn't exist.  Stop the procedure by choosing Run | Reset from the menu.

Ignoring the Error

The easiest way to avoid this problem is to add an On Error statement which will ignore the error  - if the worksheet doesn't exist, it doesn't need to be deleted in the first place!  Add an instruction to do this above the line which attempts to delete the Hit Results worksheet:

Resume Next

Use On Error Resume Next to instruct the code to ignore any run-time errors and continue from the next instruction.

 

Return to Excel and click the Get Hit Results button again to check that the code now works:

Results

After clicking the button you should see a list of films from the year that you selected on the Menu worksheet.

You can return to the Menu sheet and choose a different year then click the button again to show that the Hit Results sheet is being deleted and recreated properly.

Other Potential Problems

The error-handling code seems to be working correctly to solve the first problem we encountered, however, there is another potential issue.  When you add an On Error statement to a procedure, it affects every subsequent instruction in the procedure:

On Error

Everything below the On Error Resume Next statement is affected by it.

We've told our procedure to ignore any run-time error that occurs until the end of the subroutine!  To demonstrate why this might be a problem, return to Excel and change the name of the Menu worksheet to Main Menu:

Change name

Alter the name of the worksheet to something other than Menu.

 

Now click the Get Hit Results button again and look at the result:

Result

Regardless of which year you have selected, you'll end up with an empty list.

 

This problem occurs because code in the Get_Hit_Results subroutine refers to the Menu worksheet by name:

Menu

This instruction refers to a worksheet which no longer exists.

 

The instruction shown above causes a run-time error, but our On Error statement has told the subroutine to ignore them and so it continues to execute the code as though nothing has gone wrong!

Disabling an Error Handler

When an error handler is enabled using an On Error statement, it remains enabled until either the end of the procedure or until it is explicitly disabled.  In our example, we'd like to disable the error handler after the instruction which attempts to delete the Hit Results worksheet.  To do this, we can add another On Error statement after this instruction:

New On Error

Begin a new On Error statement below the line which deletes the Hit Results worksheet.

 

To disable the error handler in VBA, you add GoTo 0 to the end of the On Error statement:

GoTo 0

Complete the instruction by adding GoTo 0.

 

You can now click the Get Hit Results button again to check the effect of your changes:

Run-time error

You'll encounter another run-time error message.

 

Click Debug on the dialog box to check which line has caused the error:

Error line

This time, it's the instruction which refers to the Menu sheet that has caused the problem.

You can solve this problem in several ways but perhaps the best choice is to assign a code name to the worksheet using the Properties window:

Code name

Select the Main Menu worksheet in the Project Explorer and then type a code name into the (Name) property in the Properties window.

 

You can then use this new code name to reference the menu worksheet, regardless of what name appears on the worksheet tab in Excel:

Reference codename

Use the code name to refer to the worksheet.

 

To practise disabling an error handler:

  1. In Excel, click the Get Flop Results button:
Click

Click this button on the worksheet formerly known as Menu.

 
  1. Click Debug on the error message that appears:
Click debug

Click Debug to show the line that caused the error.

 
  1. Identify the instruction which caused the error and then choose Run | Reset from the menu:
Error

The line which caused the error will be highlighted in yellow.

 
  1. Add an On Error statement above the line which caused the error:
On error

Use On Error Resume Next to ignore errors caused the instructions below.

 
  1. Return to Excel and click the Get Flop Results button again:
Results

This time the code will execute without errors but won't produce any results.

 
  1. Return to the code and add an On Error statement to disable the error handler after the line which attempts to delete the Flop Results worksheet:
Disable error handler

Use On Error GoTo 0 to disable the error handler.

 
  1. Return to Excel and click the Get Flop Results button.  Click Debug on the dialog box which appears and identify the line which has caused the run-time error:
Error

The line which refers to the Menu worksheet has caused an error this time.

  1. Reset the code and edit the line which caused the error to use the code name of the menu worksheet:
Alter

Use the code name that you assigned to the worksheet earlier.

 
  1. Click the Get Flop Results button again and check that you see a list of results:
Results

The code will now produce results as expected.

  1. Save and close the workbook.
This page has 0 threads Add post