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] : 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] : 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] : 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.
Read More:
VBA Inputbox Function & Method
Instructions to Run VBA Macro Code or Procedure:
Instructions to Run VBA Macro Code or Procedure:
You can refer the following link for the step by step instructions.
Instructions to run VBA Macro Code
Other Useful Resources:
Click on the following links of the useful resources. These helps to learn and gain more knowledge.
VBA Tutorial VBA Functions List VBA Arrays in Excel VBA Tables and ListObjects
VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers Blog