VBA InputBox

The VBA InputBox prompts the user for values. The dialogue box displays a message and anticipates a response from the user in the form of a button press. On clicking OK or Enter, the InputBox method will return the inputted text to the text box. When the user presses the Cancel button, the InputBox method will give back a null value (” “). Error handling is required for the InputBox function. An error message is shown if the user presses the cancel button before inputting a value.

Syntax

InputBox(prompt, title, default, xpos, ypos, helpfile, context). All are optional parameter except prompt.

Prompt: This parameter is a string, and it helps to show a message in the dialogue box. A prompt should be at least 1024 characters in length. There should be a linefeed (Chr(10)) or carriage return (Chr(13)) between each line of text to break it.

Title: It is a string expression. The string is displayed in the dialogue box’s heading. If the left side of the title is blank, the application’s name will be inserted there.

Default: It uses the text in the textbox as the default parameter to show it to users.

XPos: The X-axis position is utilised to display the horizontal distance of the prompt from the left side of the screen. If the input box’s value is null, the box will be centred horizontally.

YPos: The Y-axis position is used to display the prompt’s vertical distance from the screen’s left edge. If this value is 0, the input box is vertically centred.

Helpfile: It is a string expression. The parameter is useful to identify the help file that tells the dialogue box what to do based on its current state.

Context: It is a number expression that helps to find the Help context number. The Help author will assign this number to a relevant help subject. The presence of the helpfile is required for providing background information.

See More: VBA VLookup

How to Create InputBox

Here are the steps you need to take in VBA to make an input box:

Step 1: Create a new module in the Visual Basic Editor.

VBA Module

Step 2: Select the newly added module by double-clicking it, and then enter the macro’s name “InputBox_Example”.

Step 3: Type the word “InputBox,” and the syntax of the InputBox will show up.

Inputbox

Step 4: Change the Prompt to “Prompt Name,” the Title to “Information,” and the Default to “Enter Here.”

InputBox Example

Example

The area of a rectangle can be determined by having the user enter values into two input boxes at runtime (one for length and one for width).

Function InputBox_Sum_Example()

Dim x As Double

Dim y As Double

x = InputBox("Enter Value ", "Enter a Number")

y = InputBox("Enter Value", "Enter a Number")

InputBox_Sum_Example = x + y

End Function

Step 1 − Use the function name and click Enter to carry out the same as in the picture below.

Function

Step 2 − First input box (Enter x Value) appears upon execution. Enter a value.

Enter x Value

Step 3 − The width input box appears after the first value has been entered.

Enter y Value

Step 4 − When you have typed in the second number, hit the OK button. The region appears as in the screenshot below.

Value

Leave a Comment

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

Scroll to Top