VBA InputBox Function

VBA InputBox Function

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.

VBA InputBox Function

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] : 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.
[XPos] and [YPos] : It is 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] : It is 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.
VBA Input Box Function

Instructions to use InputBox Function 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.

VBA InputBox Method VBA Functions List

Leave a Reply

Your email address will not be published. Required fields are marked *