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.

  1. Handling Errors in Visual Basic for Applications
  2. Customising Error-Handling Code
  3. A Worked Example - Error-Handling with InputBox
  4. Advanced Error Traps - Raising Errors and Error Bubbling (this blog)

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.

Posted by Andy Brown on 29 November 2011

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Advanced Error Traps - Raising Errors and Error Bubbling

I don't use either of these concepts much when writing VBA systems, but my gentle readers might like them.  So ...

Raising Errors

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:

Err.Raise syntax

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)


'must have solved the error - sign worksheet

Range("A1").Value = "Wise Owl"

'time to leave routine and jump over error code

Exit Sub


'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:

Raised error message

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.

 Error Bubbling

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)


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


'display final message

MsgBox "Job done!"

Exit Sub


'error-handling if sheet name wrong

MsgBox "Can not select this sheet"

Exit Sub


'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.


This blog has 0 threads Add post