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
547 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 ...
You've see how to force an Excel input box to return numeric values, but what if you want to capture a date value?
The future is so last year.
You don't need any files for this part of the lesson.
Click here to download a file which contains the completed code.
Create a new workbook and insert a new module in the VBE.
You can't use the Type parameter to limit an Excel input box to allow only date values. As a partial solution to this, you can set the input box to allow only numbers by passing a value of 1 to the Type parameter. Create a new subroutine called ReturnADate and add code which captures the result of an Excel input box in a Date variable:
Here we've restricted the input box to accept only numbers and captured its result in a Date variable.
You can run the subroutine and enter a date into the input box in a variety of different formats:
Although it doesn't look much like a number, as long as the value you enter can be evaluated as a date, it will be accepted.
You can use the Locals window to check that your date has been stored correctly:
The Locals window will show the value that you have captured.
Relying on this technique to capture dates can cause a problem if the user enters a numeric value which can't be converted into a date:
Entering a number which can't be implicitly converted into a date will cause this technique to fail.
The value in the input box shown above can't be converted into a date and so the code generates a run-time error:
You'll see this type of error message if the value can't be converted into a date.
For dates, it might be best to use the same technique you would use for a VBA input box; that is to capture the input value as a Variant before testing if it can be treated as a Date value. In the example below we've changed the data type of the variable to Variant and tested if it contains a valid date using the IsDate function:
You can use the IsDate function to test if the value entered can be converted into a date.
You can try running the subroutine again and try to enter an invalid date to check that you see a sensible message rather than a run-time error.
To practise capturing dates using an Excel input box:
Sub DoubleDate()
Dim StartDate As Date
StartDate = Application.InputBox( _
Prompt:="Enter start date", _
Type:=1)
End Sub
This is a safe date format to use.
You should see a validation message like this one after clicking OK on the input box.
This value is a number but it's outside the range of the Date data type.
This is the error message you should receive.
Sub DoubleDate()
Dim StartDate As Variant
StartDate = Application.InputBox( _
Prompt:="Enter start date")
If IsDate(StartDate) Then
StartDate = CDate(StartDate)
Else
MsgBox "That's not a date!"
Exit Sub
End If
End Sub
Sub DoubleDate()
Dim StartDate As Variant
Dim EndDate As Variant
StartDate = Application.InputBox( _
Prompt:="Enter start date")
EndDate = Application.InputBox( _
Prompt:="Enter end date")
If IsDate(StartDate) And IsDate(EndDate) Then
StartDate = CDate(StartDate)
EndDate = CDate(EndDate)
Else
MsgBox "You must enter valid dates!"
Exit Sub
End If
End Sub
Sub DoubleDate()
Dim StartDate As Variant
Dim EndDate As Variant
StartDate = Application.InputBox( _
Prompt:="Enter start date")
EndDate = Application.InputBox( _
Prompt:="Enter end date")
If IsDate(StartDate) And IsDate(EndDate) Then
StartDate = CDate(StartDate)
EndDate = CDate(EndDate)
Else
MsgBox "You must enter valid dates!"
Exit Sub
End If
MsgBox _
"Difference in days = " & _
DateDiff("d", StartDate, EndDate)
End Sub
Try entering your birth date followed by the current date to see a horrifyingly large number.
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.