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 ...
If you want to avoid type mismatch errors when capturing numbers or dates from an input box but you don't want the extra work of declaring two variables for each value, you can use Variant variables instead:
Creating two variables for each value feels a little too much like hard work.
You don't need any files for this section.
You can click here to download the sample code.
Create a new workbook and insert a new module in the VBE.
If you'd rather not declare multiple variables for each value you want to store, you could use a single variable with the Variant data type. Create a new subroutine called UsingVariant and add code to capture the result of an input box in a Variant variable:
Declaring a variable with the Variant type means that you can store any type of value in it.
After capturing the value entered into the input box, the variable will have a subtype of String:
You can see the subtype of Variant variables in the Locals window.
You can then check if the value can be treated as a date and, if so, convert the subtype of the variable using the CDate function:
You set the value of the DoB variable to be equal to the converted version of itself!
After converting the variable in this way, it will have a subtype of Date:
Check the Locals window to see the new subtype of the variable.
Although this technique uses fewer variables it does have a couple of drawbacks: it's less easy to tell, at a glance, what data type the variable will have at any given point, and; you might inadvertently alter the subtype of the variant in another part of your code. Ultimately, of course, the technique you choose is up to you!
To practise using Variant variables:
Sub BeMyBMI()
Dim HeightM As Variant
Dim WeightKg As Variant
HeightM = InputBox( _
"Enter height in metres")
WeightKg = InputBox( _
"Enter weight in kg")
End Sub
Sub BeMyBMI()
Dim HeightM As Variant
Dim WeightKg As Variant
HeightM = InputBox( _
"Enter height in metres")
If Not IsNumeric(HeightM) Then
MsgBox "Your height must be a number!"
Exit Sub
End If
WeightKg = InputBox( _
"Enter weight in kg")
If Not IsNumeric(WeightKg) Then
MsgBox "Your weight must be a number!"
Exit Sub
End If
End Sub
Choose View | Locals Window from the menu if you can't see it.
Sub BeMyBMI()
Dim HeightM As Variant
Dim WeightKg As Variant
HeightM = InputBox( _
"Enter height in metres")
If Not IsNumeric(HeightM) Then
MsgBox "Your height must be a number!"
Exit Sub
End If
WeightKg = InputBox( _
"Enter weight in kg")
If Not IsNumeric(WeightKg) Then
MsgBox "Your weight must be a number!"
Exit Sub
End If
HeightM = CDbl(HeightM)
WeightKg = CDbl(WeightKg)
End Sub
You can see this information in the Locals window.
Sub BeMyBMI()
Dim HeightM As Variant
Dim WeightKg As Variant
Dim BMI As Double
HeightM = InputBox( _
"Enter height in metres")
If Not IsNumeric(HeightM) Then
MsgBox "Your height must be a number!"
Exit Sub
End If
WeightKg = InputBox( _
"Enter weight in kg")
If Not IsNumeric(WeightKg) Then
MsgBox "Your weight must be a number!"
Exit Sub
End If
HeightM = CDbl(HeightM)
WeightKg = CDbl(WeightKg)
BMI = WeightKg / (HeightM * HeightM)
MsgBox _
"Height: " & HeightM & "m" & vbNewLine & _
"Weight: " & WeightKg & "kg" & vbNewLine & _
"BMI: " & Format(BMI, "0.00"), _
vbInformation
End Sub
If you type numbers into each of the input boxes you'll see a message resembling this one.
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.