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 ...
Although the VBA input box always returns a string, you can use it to capture values of other data types too:
You can enter dates and numbers into an input box and capture them as a non-string data type.
You can click here to download the file needed for this part of the lesson.
You can click here to download a file containing the sample code.
The VBA InputBox function always returns the value you enter as a string. You can, however, capture the return value as a different data type simply by storing it in a variable with the appropriate type.
Extract and open the workbook linked to in the Files Needed section above. In Module1 find the subroutine called CaptureDifferentDataTypes. The sdubroutine asks a user to enter a string, a date and a number into three separate input boxes:
You can ask users to enter any data type, although the result will always be returned as a string.
When you run the subroutine, you can enter a value of the appropriate type into each input box:
You can enter a date in a variety of different formats.
You must enter a value of the appropriate type into the input box, otherwise you'll encounter a type mismatch run-time error. This will also happen if you choose to cancel the input box.
If you step through the procedure using the F8 key, you can see the values stored with the appropriate data type using the Locals window (choose View | Locals Window from the menu if you can't see it):
Providing you type a valid value into each input box it will be captured with the appropriate data type.
This process works because of a feature of the VBA language referred to as implicit data type conversion. In simple terms, this means that if a value can be converted from one data type to another, VBA will do this automatically. The animation below shows this process with three different data types. You can follow along using the subroutine called ImplicitDataTypeConversion in Module1:
The first value is entered into a String variable.
To practise capturing non-string values:
Sub WhatsMyBMI()
Dim HeightM As Double
Dim WeightKg As Double
End Sub
Sub WhatsMyBMI()
Dim HeightM As Double
Dim WeightKg As Double
HeightM = InputBox("Enter height in metres")
WeightKg = InputBox("Enter weight in kilograms")
End Sub
You can step through the procedure by pressing the F8 key and use the Locals window to check the values have been stored (choose View | Locals Window from the menu if you can't see it).
Remember that if you don't enter a number into each input box you'll receive a run-time error message!
Sub WhatsMyBMI()
Dim HeightM As Double
Dim WeightKg As Double
Dim BMI As Double
HeightM = InputBox("Enter height in metres")
WeightKg = InputBox("Enter weight in kilograms")
BMI = WeightKg / (HeightM * HeightM)
End Sub
Sub WhatsMyBMI()
Dim HeightM As Double
Dim WeightKg As Double
Dim BMI As Double
HeightM = InputBox("Enter height in metres")
WeightKg = InputBox("Enter weight in kilograms")
BMI = WeightKg / (HeightM * HeightM)
MsgBox _
"Height: " & HeightM & "m" & vbNewLine & _
"Weight: " & WeightKg & "kg" & vbNewLine & _
"BMI: " & Format(BMI, "0.00"), _
vbInformation, _
"What's my BMI?"
End Sub
If you were curious what the number means you could check it against the categories listed here.
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.