spot_img

VBA InputBox

VBA InputBox is used to prompt the user to enter the values. This message box is used to displaying a message and waits for the user action performed by pressing the button.

A text can be return in the text box by using the InputBox function if the user clicks on the OK or Enter button. And if the user clicks on the Cancel button, then the InputBox function will return an empty string (” “).

InputBox function needs proper error handling. If the user clicks the cancel button without entering any value, then it displayed an error.

Syntax

  • Prompt (required parameter): This parameter represents a string, and it is used to display as a message in the dialog box. The minimum length of prompt is 1024 characters approximately. If the length of the text is increased, then break the text by using a linefeed character (Chr(10)) or carriage return character (Chr(13)) between each line of the text.
  • Title (optional parameter): The title is a string expression. It is used to display the string in the title bar of the dialog box. And the application name is placed in the title bar if the left side of the title is empty.
  • Default (optional parameter): It takes the text as a default parameter in the textbox to display it to the users.
  • XPos (optional parameter): The position of the X-axis is used to show the prompt distance from the left side of the screen as horizontally. The input box is centered horizontally if its value is null.
  • YPos (optional parameter): The position of the Y-axis is used to show the prompt distance from the left side of the screen as vertically. , then the input box is centered vertically If its value is blank.
  • Helpfile (optional parameter): The helpfile is a string expression. This parameter is used to identify the help file that provides context-sensitive help to the dialog box.
  • Context (optional parameter): It is a numeric expression that is used to identify the Help context number. This number is assigned to a suitable help topic by the Help author. The helpfile must be present if we want to provide the context.

How to Create InputBox

To create the InputBox in VBA, follow the below steps, such as:

Step 1: Go to the Visual Basic Editor and insert a new module.

VBA InputBox

Step 2: Double click on the recently added module and write the macro name, i.e., InputBox_Example.

VBA InputBox

Step 3: Write the word “InputBox“, and we will see the syntax of the InputBox.

VBA InputBox

Step 4: Now give the Prompt as “Your Name“, title as “personal information“, and Default as “Enter Here“.

VBA InputBox

Step 5: Run the code using the Run button.

VBA InputBox

InputBox Type Parameter Options

Number Type of Value
0 Only Formula can be entered.
1 The only number can be entered.
2 Only text can be entered.
4 Only Logical value (True or False) can be entered.
8 Only select the cell reference.
16 Only error values entered.
64 An array of the values.

Example

We use the popular features of an InputBox function shown in the following example, such as:

VBA InputBox

  • This code asks for the input with the help of an InputBox and assigns it to a variable.
  • It verifies the code; otherwise, it exists.

Now run the above code using the Run button, and we will get the result in a message box, such as:

VBA InputBox

While the dialog box is displayed, then the user has to press one of the buttons.

When the user presses any one button from the displayed buttons, then a text string is return with the help of the InputBox function.

  • If the user clicks on the OK button, then the text string has been entered into the dialog box.
  • If the user clicks on the Cancel button, then an empty text string is returned.

In the above example, the returned text string is assigned to the MyInput variable before the execution of the code.


Next TopicVBA-M

spot_img
Previous articleVBA Data Types
Next articleCircular reference in Excel