VBA InputBox Method
VBA InputBox Method helps to prompt the user to enter a value(s) or information. It is a method of Excel application method. Once value is entered in the Input Box, you can press either OK or enter button from keyboard or Cancel button. If we click cancel button it returns an empty string. It is a Built-In method in VBA. In Excel it returns a variant value and stored in a variable. If user doesn’t enter any value and clicks on OK button, it returns zero length string. You can specify data type of input in VBA. You have an option to define the type of data. This is one of the advantage here.
VBA InputBox Method Syntax
Here is the Syntax of InputBox method in VBA. It has one mandatory parameter and seven optional parameters. It returns number, string, formula, cell reference, or range.
InputBox Method :
Expression.InputBox(Prompt, [Title], [Default], [Left], [Top], [HelpFile], [HelpContextID], [Type])
Where
Expression : It represents application in the InputBox method
Prompt : It is a mandatory argument. It contains string data type and consists of 1024 characters. It represents the message that is displayed in the dialog box.
[Title] : It is an optional argument and contains variant data type. It represents a title displayed in the title bar of the dialog box. Default name is application name.
[Default] : It is an optional argument and contains variant data type. It represents a default value displays in the text box.
[Left] and [Top] : It is an optional argument and contains variant data type. It represents the Left and Top position of the textbox. Default displays on the center of the screen.
[HelpFile] : It is an optional argument and contains variant data type. It represents name of the help file. The help button displays on the inputbox.
[HelpContextID] : It is an optional argument and contains variant data type. It represents the number of the topic in help file.
[Type] : It is an optional argument and represents the data type of the InputBox. The default value is ‘8’. This helps user to allow numbers, string, formula, cell reference or Range. You can find list of types in the following table.
Value | Description |
---|---|
0 | Accepts a formula |
1 | Accepts a number |
2 | Accepts a Text / String |
4 | Accepts a logical value True or False |
8 | Accepts a cell reference as a range |
16 | An error value such as #N/A |
64 | Accepts an array of values |
VBA InputBox Method Example Macro Code
Let us see the example macro for better understand on on InputBox Method using VBA.
'Select Cell Value from Worksheet using VBA InputBox Method Sub VBAF1_Select_Value_From_Sheet_Using_Inputbox_Method() 'Variable declaration Dim iCellVal As Integer iCellVal = Application.InputBox("Select a cell: ", "VBAF1", , , , , , 8) MsgBox "Selected cell value : " & iCellVal, vbInformation, "VBAF1" End Sub
Output: Here is the output screenshot of above example macro code.
Instructions to use InputBox Method example macro
You can follow step by step instructions to run above macro example VBA code.
- Open Visual Basic Editor(VBE) by clicking Alt +F11
- Go to code window by clicking F7
- Copy above specified macro or procedure
- Paste above copied code in code window
- Run macro by clicking F5 or Run command
- Now you can see the Input Box method example output on the system screen..
Other Related References
Click on the following links for related reference articles. It helps for your reference.