The VBA InputBox Function

Related Function:
VBA MsgBox

Description

The VBA InputBox function displays a dialog box, prompting the user for input, and containing an OK button and a Cancel button.

The function returns a text string containing the user's input if the OK button is selected or an empty text string if the Cancel button is selected.

The syntax of the InputBox function is:

InputBox( Prompt, [Title], [Default], [XPos], [YPos], [HelpFile], [Context] )

Where the function arguments are:

Prompt - The text string that you want to appear in the input box.
[Title] - A optional text string that specifies a title to be displayed at the top of the input box.
[Default] - A optional text string that is displayed in the input box as the default response if no other response is entered.
[XPos] - An integer specifying (in twips) the horizontal distance of the input box, from the left edge of the screen.
[YPos] - An integer specifying (in twips) the vertical distance of the input box, from the top of the screen.
[HelpFile] -

An optional string argument, identifying the Help file relating to the input box.

If the [HelpFile] argument is provided, the [Context] argument must also be provided.
[Context] -

An optional numeric value that is the context ID for the Help topic relating to the input box.

If the [Context] argument is provided, the [HelpFile] argument must also be provided.


VBA InputBox Function Examples

Example 1

' Request the user's name.
Dim response As String
response = InputBox( "Please enter your name" )
' The text string "response" now contains the user's input (or an empty string
' if the Cancel button was selected).

The above call to the InputBox function displays the following dialog box to the user:

Example of a Simple Input Box, Requesting the User's Name

While the dialog box is displayed, the VBA code pauses, until the user selects one of the buttons.

Once the user selects one of the dialog box buttons, the InputBox function returns a text string, as follows:

In the above example code, the returned text string is then assigned to the variable response before the program continues to run.


Example 2

' Request a reference.
Dim response As String
response = InputBox( "Please enter a reference for the current data set", "Reference Request", "Ref" )
' The text string "response" now contains the user's input (or an empty string
' if the Cancel button was selected).

The above code displays the following input box to the user:

Example of an Input Box, Requesting a Reference, With a Default Value

Note that in the above VBA code: