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:
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. |
' Request the user's name.
Dim response As String
' The text string "response" now contains the user's input (or an empty stringresponse = InputBox( "Please enter your name" ) ' if the Cancel button was selected). |
The above call to the InputBox function displays the following dialog box to the user:
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.
' Request a reference.
Dim response As String
' The text string "response" now contains the user's input (or an empty stringresponse = InputBox( "Please enter a reference for the current data set", "Reference Request", "Ref" ) ' if the Cancel button was selected). |
The above code displays the following input box to the user:
Note that in the above VBA code:
Once again, when the user selects one of the dialog box buttons, the InputBox function returns a text string, as follows: