InputBox function VBA – Syntax and Examples

In the previous article, we learned about the MsgBox Function. MsgBox Function is helpful for output while the InputBox function in VBA is useful for input. So let us start learning.

Input Box in VBA – InputBox Function

An Input Box is a dialog box that is used to input a value. We use the InputBox Function to show an input box to take input from the user. An input box comes with two buttons on it and a text box. If the user clicks on the Ok button, the InputBox function returns the input value entered in the text box to the VBA code. The second is the Cancel button. When a user clicks on the Cancel button, the function returns an empty text string (“”).

We can also specify a default value for the input variable. When user closes the Input Box without giving any input, the default value is returned.

Syntax – InputBox Function

<input_variable>=InputBox(Prompt,[Title],[Default],[XPos],[YPos],[HelpFile],[Context])

<input_varable> contains the value returned by the InputBox function. The parameters in the square brackets are optional. Only the Prompt is Compulsory to call the InputBox function.

Below is the explanation of all the parameters required by the InputBox function.

  • Prompt − This is the message that is shown in the Input Box which directs user about the input to be given. The maximum length is 1024 characters.
  • [Title] − This is the title of the Input Box at the top left corner. If the Title is not specified, we see the Application name.
  • [Default] − This is the default value of the input variable.
  • [XPos] − This is the distance of Input Box from the left side of the screen on x axis in pixels.
  • [YPos] − This is the distance of Input Box from the top of screen on the y axis in pixels.
  • [HelpFile] − An optional parameter. A String expression that identifies the helpfile to be used to provide context-sensitive Help for the dialog box.
  • [Context] − An optional parameter. If context is provided, helpfile must also be provided.
infpgraphics inputbox function in vba

Examples of Input Box Function in VBA

In this section of the blog, we would do some examples to implement the InputBox Function to take user input.

Example 1 – Get Length and Breadth to Get Area of Rectangle

In this example, we would input the value of length variable l and breadth variable b to get the area of rectangle in variable a.

Sub Area()
'declaring l,b and a
Dim l As Integer, b As Integer, a As Integer
'input the length l
l = InputBox("Enter the Length of Rectange:=")
'input the breadth b
b = InputBox("Enter the breadth of Rectangle=")
'getting area a
a = l * b
'printing the value of area
MsgBox ("Area of Rectangle is:= " & a)
End Sub

Run this Sub Procedure to see how the input box appears.

Using an Input Box function to get the area of rectangle in vba

Example 2 – Login by Inputting Password, Setting a Default value

When the User Clicks on the Ok button in the Input Box, without giving any input, then it returns an empty text string. If the input variable is not of string type, then it would lead to an error.

We can prevent this by adding a default value of the input variable to be returned by Input Box.

A default value appears in the Text Box like a saved value. Moreover, the user can change if he/she wants to.

Sub login()
'creating a login form
Dim roll As Integer, password As String
'input the roll number, default value is 11039
roll = InputBox("Enter your roll number:= ", "LogIn", 11039#)
'input the password, default value is 00000000
password = InputBox("Enter Password:= ", "LogIn", "00000000")
'checking if the password is correct (equal to 12345678)
If password = "12345678" Then
    x = MsgBox("Welcome", vbInformation)    'correct
Else
    x = MsgBox("Incorrect Password", vbCritical)    'incorrect
End If

End Sub

You can see that we also specify the Title as “LogIn” in the InputBox parameter. The input variables are roll and then password.

Using an Input Box function to get the password and specify the saved password

When we run the procedure, the roll number is 11039 by default. We enter the password as 12345678 and it logs in. However, the default password 00000000 is incorrect so it shows an incorrect password message.

This brings us to end.

Thank you for reading.❤

Leave a Comment