Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
547 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
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.
You can click here to download the file for this part of the lesson.
You can click here to download a file containing the sample code.
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:
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.
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:
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:
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.
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:
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:
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.
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:
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:
Alter the name of the worksheet to something other than Menu.
Now click the Get Hit Results button again and look at the 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:
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!
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:
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:
Complete the instruction by adding GoTo 0.
You can now click the Get Hit Results button again to check the effect of your changes:
You'll encounter another run-time error message.
Click Debug on the dialog box to check which line has caused the error:
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:
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:
Use the code name to refer to the worksheet.
To practise disabling an error handler:
Click this button on the worksheet formerly known as Menu.
Click Debug to show the line that caused the error.
The line which caused the error will be highlighted in yellow.
Use On Error Resume Next to ignore errors caused the instructions below.
This time the code will execute without errors but won't produce any results.
Use On Error GoTo 0 to disable the error handler.
The line which refers to the Menu worksheet has caused an error this time.
Use the code name that you assigned to the worksheet earlier.
The code will now produce results as expected.
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.