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
551 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 ...
Up to this point we've assumed that the user always clicks OK on the input box. This section explains what happens if Cancel is clicked instead.
What happens to the deep and profound answer we've written if we click the Cancel button?
Click here to download the file you need for the first part of this topic.
You can click here to download a file containing the sample code.
Extract and open the file linked to in the Files Needed section above. In the VBE, find the subroutine called CancellingAnInputBox in Module1. The code in the subroutine asks the user to enter their name and then presents a message box with a simple greeting:
The basic code to ask a question and display a response.
If you click Cancel on an input box it will return an empty string, regardless of whether you've already typed something:
You can click Cancel to abandon the input box even if you've already typed something.
Cancelling the input box doesn't prevent the rest of the procedure from running. In our simple example, the message box will still appear but without displaying a name:
The message box concatenates "Hello " with the empty string stored in the variable.
You can test if the input box has been cancelled by checking if the variable contains an empty string:
This code will display a different message and exit from the procedure if the user cancels the input box.
Cancelling the input box this time will result in a different message appearing:
This is preferable to the partially complete message from earlier.
To practise cancelling an input box:
Click the Actor Search button, enter a name in the input box and click OK.
You should see something similar to this if the actor you have searched for is in the list.
This fairly useless message appears if you cancel the input box.
Sub ActorSearch()
Dim SearchName As String
Dim ActorDoB As Date
Dim ActorCell As Range
'Ask the user which name to search for
SearchName = InputBox( _
"Enter an actor name")
'TODO
'Test if the SearchName variable contains an empty string
'If so, show a message and exit the procedure
If SearchName = "" Then
End If
Sub ActorSearch()
Dim SearchName As String
Dim ActorDoB As Date
Dim ActorCell As Range
'Ask the user which name to search for
SearchName = InputBox( _
"Enter an actor name")
'TODO
'Test if the SearchName variable contains an empty string
'If so, show a message and exit the procedure
If SearchName = "" Then
MsgBox _
"You didn't enter a name!", _
vbExclamation
Exit Sub
End If
Your user should now be able to tell what went wrong.
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.