Using InputBox in Excel VBA

InputBox is an inbuilt function in Excel VBA that is used to get a value from the user. InputBox asks the user to enter the value and then can store the value in the variable of same data type. Here, in this post, we are illustrating the example of Excel Macro InputBox to store the input value in the excel cell.

Below is the syntax of InputBox function.

InputBox(Prompt, Title, Default)

Prompt: The question of the input box.

Title: This is optional. The title or heading of the input box.

Default: This is optional. The value of this parameter appears in the typing area of InputBox.

VBA InputBox in Excel Macro

Steps to Create InputBox in Excel VBA

Step1: Go to Visual Basic Editor. Insert the Module.

Step2: Write a procedure with the name Example_InputBox.

Sub Example_InputBox ()

End Sub

Step3: Declare the variable with the name yourName of variant data type. (Here we are taking variable of variant data type as it can store input value of any data type such as string, integer, char etc.)

Dim yourName As Variant

Step4: Write the below given code in the procedure.

yourName = InputBox("Please Enter Your Name", "Your Information", "Type Here")

Step5: Write the below given code that will store the typed value in the cell A2.

Range("A1").Value = yourName

Step6: Run the code. You will see the InputBox asking you to enter your name. Enter your name and click OK. You will see that the entered value has been stored in the cell A1.

The complete code of above example will look as below given.

Sub Example_InputBox()

Dim yourName As Variant

yourName = InputBox("Please Enter Your Name", "Your Information", "Type Here")

Range("A1").Value = yourName

End Sub

Another Example of Creating Macro of VBA InputBox

Below code example will display one InputBox for accepting Name & storing in yourName variable and the other for accepting Contact Number & storing in yourContact variable. Then Name and Contact Number will be stored in A1 and B1 cells.

Sub Example_InputBox()

Dim yourName As Variant
Dim yourContact As Variant
yourName = InputBox("Please Enter Your Name", "Your Information", "Type Here")
yourContact = InputBox("Please Enter Your Contact Number", "Your Information", "Type Here")
Range("A1").Value = yourName
Range("B1").Value = yourContact

End Sub

Leave a Reply