Module 5 - Messages and User Inputs
Lesson 5.3 - The VBA Input Box
Topic 5.3.4 - Capturing Numbers/Dates

Although the VBA input box always returns a string, you can use it to capture values of other data types too:

Enter date

You can enter dates and numbers into an input box and capture them as a non-string data type.

 

Files Needed

You can click here to download the file needed for this part of the lesson.

Completed Code

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.

Capturing Different Data Types

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:

Asking for different types of input

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:

Entering dates

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):

Locals window

Providing you type a valid value into each input box it will be captured with the appropriate data type.

 

Why This Works

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:

Converting data types

The first value is entered into a String variable.

 

To practise capturing non-string values:

  1. Using the same module, create a subroutine called WhatsMyBMI.
  2. Declare two Double variables to hold a person's height in metres and weight in kilograms:

Sub WhatsMyBMI()

 

Dim HeightM As Double

Dim WeightKg As Double

 

End Sub

  1. Use input boxes to ask the user to enter their height and weight and store the results in the variables you have declared:

Sub WhatsMyBMI()

 

Dim HeightM As Double

Dim WeightKg As Double

 

HeightM = InputBox("Enter height in metres")

WeightKg = InputBox("Enter weight in kilograms")

 

End Sub

  1. Run the subroutine and check that the values you enter are stored correctly:
Check values

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!

  1. Add a new Double variable and use it to calculate the body mass index of the user (the formula is shown in the code below):

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

  1. Display a message box to tell the user what their BMI is:

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

  1. Run the subroutine to check that it produces a result:
BMI

If you were curious what the number means you could check it against the categories listed here.

 
  1. Save and close the file.
This page has 0 threads Add post