563 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owls 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 ...
Error-handling and error trapping in Excel Visual Basic macros Part four of a four-part series of blogs |
---|
What happens when your macros go wrong? That depends what error-handling you have in place. Learn how to use ON ERROR and other commands to trap errors in a structured way.
This is one small part of our free online Excel VBA tutorial. To find out how to learn in a more structured way, have a look at our training courses in VBA.
|
I don't use either of these concepts much when writing VBA systems, but my gentle readers might like them. So ...
If system errors aren't enough for you, why not create your own? Although your first reaction to this proposal might be one of disbelief ("why on earth would I ever want to do that"?), it can be useful.
The command in question is as follows:
You can specify an error number, the source of the error and a message.
Since these things always make more sense with examples, here's our code to select a worksheet (again!), but this time raising an error when a problem occurs:
Sub SignatureWithErrorCorrection()
Const SheetName As String = "TestSheet"
'if an error happens, solve it and try again
On Error GoTo NoWorksheet
'now try going to worksheet (may trigger error)
Worksheets(SheetName).Select
'must have solved the error - sign worksheet
Range("A1").Value = "Wise Owl"
'time to leave routine and jump over error code
Exit Sub
NoWorksheet:
'crash system, but with meaningful error
Err.Raise _
Number:=vbObjectError + 1, _
Source:="Crashed in " & Err.Source, _
Description:="Tried to go to worksheet " & _
UCase(SheetName) & ", which doesn't exist"
End Sub
Here's the error message you'll see if the TestSheet worksheet doesn't exist when you run this routine:
The error message shows the number of your error and the description.
Microsoft recommend that you add vbObjectError to your own error messages to avoid conflict with system errors.
What happens when an unhandled error occurs in a lower-level subroutine? Let's try that question again in English. Suppose that we have two routines created - one to select a worksheet, and one to type something into cell A1:
Sub SelectSheet(SheetName As String)
'now try going to worksheet (may trigger error)
Worksheets(SheetName).Select
End Sub
Sub SignSheet()
'sign cell A1 in the active sheet
Range("A1").Value = "Wise Owl"
End Sub
Note that neither routine contains any error-handling. Now consider a calling routine which does:
Sub SignSheetOneLastTime()
'select the correct sheet
On Error GoTo NotSelected
SelectSheet "TestSheet"
'sign cell A1
On Error GoTo NotSigned
SignSheet
'display final message
MsgBox "Job done!"
Exit Sub
NotSelected:
'error-handling if sheet name wrong
MsgBox "Can not select this sheet"
Exit Sub
NotSigned:
'error-handling if can not type into cell
MsgBox "Can not sign this cell"
Exit Sub
End Sub
When you run the SignSheetOneLastTime macro above, if an error occurs in the SelectSheet macro which is not handled there, it will bubble up into the calling routine.
Thus one approach to error-handling is to incorporate all of your error traps in the top-level calling routine!
Of course, another approach is to write perfect code - but that's where we came in with this blog, so it must be time to finish.
Parts of this blog |
---|
|
Some other pages relevant to the above blogs include:
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 2023. All Rights Reserved.