WiseOwl Training - Established 1992 Wise Owl Training

Established May 1992
30 years in business
Wise Owl Training
30 years in business
See 525 reviews for our classroom and online training
Error-handling and error trapping in Excel Visual Basic macros
Part three 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 (this blog)
  4. Advanced Error Traps - Raising Errors and Error Bubbling

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.

A Worked Example - Error-Handling with InputBox

A common requirement in VBA programs is to display an input box, and get the user to type in an integer.  Here is a routine which will allow you to do just that:

Function GetInteger() As Long

Dim InputString As String

'first get something from user

InputString = InputBox("Input a number")

'if it's empty, user chose CANCEL

If Len(InputString) = 0 Then

GetInteger = -1

Exit Function

End If

'otherwise, try to convert to integer

On Error GoTo NotInteger

GetInteger = CLng(InputString)

'if this worked, exit

Exit Function


'if the conversion failed, return error

GetInteger = -1

End Function

Here is how you could call this routine to get a whole number (integer), then display its square:

Sub SquareNumber()

Dim NumberToSquare As Long

'get the number to take square of

NumberToSquare = GetInteger

'if this isn't an integer, say so

If NumberToSquare = -1 Then

MsgBox "You must enter an integer"

Exit Sub

End If

'otherwise, display answer

MsgBox "Square of " & NumberToSquare & _

" is " & (NumberToSquare ^ 2)

End Sub

When you run the SquareNumber routine, if you enter text into the input box it will trigger an error and pass control to the NotInteger label.  This in turn will set the value of the function to -1.

InputBox for entering integer

If you type in text like this, the code will trigger an error when you try to convert it to an integer.


You will then see an error message like this:

Message box displaying error

The message which will appear if you leave the input box blank or type in something which isn't a whole number.


This is a typical use of error trapping:

  • We know we are about to do something which sometimes won't work;
  • We set an error trap giving a label to jump to in the event of an error;
  • We run the code, secure in the knowledge that it will do the right thing whether or not the user reacts in the correct way.

That's pretty much the complete story about error-trapping, but error-trapping aficionados might like to read the final part of this series, in which you can raise your own errors and learn how error messages bubble up through the call stack!


This blog has 0 threads Add post