VBA InputBox Function helps to prompt the user to enter a value(s) or information. 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 dialog box in VBA. In Excel it returns only a string type data an d stored in a variable. If user doesn’t enter any value and clicks on OK button, it returns zero length string. You can covert this string value using VBA conversion functions to suite your requirements.
Syntax of VBA Input Box Function
Let us see the Syntax of Input Box Function in VBA. It has one mandatory parameter and six optional parameters. It returns a string type data.
InputBox Function :
InputBox(Prompt, [Title], [Default], [XPos], [YPos], [HelpFile], [Context] as String)
Where
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] : 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.
[XPos] and [YPos] : An optional argument and contains variant data type. It represents the X-axis and Y-axis 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 input box.
[Context] : An optional argument and contains variant data type. It represents the number of the topic in help file.
VBA Input Box Function Example Macro Code
Let us see the example macro for better understand on on InputBox Function using VBA.
'Sum of two values using VBA InputBox Function Sub VBAF1_Find_Sum_Of_Two_Values_Using_Inputbox_Function() 'Variable declaration Dim iFirstVal As Integer Dim iSecondVal As Integer Dim iResult As Integer iFirstVal = InputBox("First Value", "VBAF1") iSecondVal = InputBox("Second Value", "VBAF1") iResult = iFirstVal + iSecondVal MsgBox "Sum of two values : " & iResult, vbInformation, "VBAF1" End Sub
Output: Here is the output screenshot of above example macro code.
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 Blog
VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers