- VBA - Userforms
- VBA - Programming Charts
- VBA - Text Files
- VBA - Excel Objects
- VBA - Error Handling
- VBA - Events
- VBA - Sub Procedure
- VBA - Functions
- VBA - Arrays
- VBA - Date and Time
- VBA - Strings
- VBA - Loops
- VBA - Decisions
- VBA - Operators
- VBA - Constants
- VBA - Variables
- VBA - Input Box
- VBA - Message Box
- VBA - Macro Comments
- VBA - Excel Terms
- VBA - Excel Macros
- VBA - Overview
- VBA - Home
VBA Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
VBA - InputBox
The InputBox function prompts the users to enter values. After entering the values, if the user cpcks the OK button or presses ENTER on the keyboard, the InputBox function will return the text in the text box. If the user cpcks the Cancel button, the function will return an empty string ("").
Syntax
InputBox(prompt[,title][,default][,xpos][,ypos][,helpfile,context])
Parameter Description
Prompt − A required parameter. A String that is displayed as a message in the dialog box. The maximum length of prompt is approximately 1024 characters. If the message extends to more than a pne, then the pnes can be separated using a carriage return character (Chr(13)) or a pnefeed character (Chr(10)) between each pne.
Title − An optional parameter. A String expression displayed in the title bar of the dialog box. If the title is left blank, the apppcation name is placed in the title bar.
Default − An optional parameter. A default text in the text box that the user would pke to be displayed.
XPos − An optional parameter. The position of X axis represents the prompt distance from the left side of the screen horizontally. If left blank, the input box is horizontally centered.
YPos − An optional parameter. The position of Y axis represents the prompt distance from the left side of the screen vertically. If left blank, the input box is vertically centered.
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. A Numeric expression that identifies the Help context number assigned by the Help author to the appropriate Help topic. If context is provided, helpfile must also be provided.
Example
Let us calculate the area of a rectangle by getting values from the user at run time with the help of two input boxes (one for length and one for width).
Function findArea() Dim Length As Double Dim Width As Double Length = InputBox("Enter Length ", "Enter a Number") Width = InputBox("Enter Width", "Enter a Number") findArea = Length * Width End Function
Output
Step 1 − To execute the same, call using the function name and press Enter as shown in the following screenshot.
![Input Box Demo](/vba/images/vba_inputbox_1.jpg)
Step 2 − Upon execution, the First input box (length) is displayed. Enter a value into the input box.
![Input Box Demo](/vba/images/vba_inputbox_2.jpg)
Step 3 − After entering the first value, the second input box (width) is displayed.
![Input Box Demo](/vba/images/vba_inputbox_3.jpg)
Step 4 − Upon entering the second number, cpck the OK button. The area is displayed as shown in the following screenshot.
![Input Box Demo](/vba/images/vba_inputbox_4.jpg)