VBA - InputBox


The InputBox function prompts the users for the input values. After entering the values, if the user clicks the OK button or press ENTER on the keyboard, the InputBox function will return the text in the text box. If the user clicks the Cancel button, the function will return an empty string (""), which can be used to validate if the user have entered any value in inputbox or not.


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 line, then the lines can be separated using a carriage return character (Chr(13)) or a linefeed character (Chr(10)) between each line.
  • Title − It is An optional parameter. A String expression that displayed on the title bar of the dialog box. If the title is left blank, then the application name is placed in the title bar.
  • Default − It is an optional parameter. A default text in the text box that the user would like to be displayed.
  • XPos − It is an optional parameter. The position of X axis represents the prompt distance from the left side of the screen horizontally. If not assigned, the input box placed in horizontally centered.
  • YPos − it is an optional parameter. The position of Y axis represents the prompt distance from the left side of the screen vertically. If not assigned, the input box is vertically centered.
  • Helpfile − it is an optional parameter. A String expression that identifies the help file 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 Code

Let us create the Full name of the user by getting values from the user at run time with the help of two input boxes 
Step 1 − To execute the same, press F8 and selecting macro using the name Or go to visual basic studio by pressing atl+F11 and select the macro and press F5

Step 2 − on execution, the First input box (First Name) is displayed. Enter a value into the input box.

Step 3 − After entering the first value, the second input box (Last Name) is displayed.

Step 4 − on entering the second number, click the OK button. The Full Name is displayed as shown in the following screenshot.

Post a Comment

0 Comments