BLOGS BY TOPIC▼
BLOGS BY AUTHOR▼
BLOGS BY YEAR▼
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.
- Handling Errors in Visual Basic for Applications
- Customising Error-Handling Code
- A Worked Example - Error-Handling with InputBox (this blog)
- 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
NotInteger:
'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.

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:

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!
- Handling Errors in Visual Basic for Applications
- Customising Error-Handling Code
- A Worked Example - Error-Handling with InputBox (this blog)
- Advanced Error Traps - Raising Errors and Error Bubbling