- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
VBA: How to use an Inputbox
What is an input box?
An Input Box is a standard form built into visual basic that allows a program to prompt input from the end user.
If the user inputs a value in the box and clicks "OK" the value is return to visual basic. If the user clicks "Cancel" or closes the window the input box returns "False"
Storing User Input
Data from the user must be stored in a variable. This variable can then be referenced further in code.
It is best to define the type of variable before storing user input. Click here for help declaring variables.
Syntax
The proper syntax for message box is as follows:
Variable=inputbox(Prompt, Title, Default, Xpos, Ypos, HelpFile, ContextId)
The most commonly used variants are the prompt and the title.
Prompt: This is the only required variant for the input box. This is typically the text that directs the user what to input.
Title: This is an optional input that allows the user to control the title at the top of the window. If not defined it will typically default to the application name. As shown in the illustration above. The title bar shows "Microsoft Excel"
A Simple Example
The example below works in MS Excel. The user is prompted to input their name. It continues to prompt if the user input is blank or they click cancel.
Sub PromptName() Dim strName As String Do Until strName <> "" And strName <> "False" strName = InputBox("Please Input your Name", "Name Prompt") Loop Range("A1") = "My Name is " & strName End Sub
Congratulations
You are now on your way to learning VBA. If you are new to VBA and would like to learn more. Please visit my other articles. Have a great day!