Excel VBA InputBox
VBA InputBox is inbuilt function used to get a value from the user, this function has two major arguments in which one is the heading for the input box and another is the question for the input box, input box function can store only the data types input which it variable can hold.
Often in excel, we use the data which is already there in the excel sheet. Sometimes we need some kind of input data from users as well. Especially in VBA, input from the user is required oftentimes.
Using the InputBox, we can get the data from the user and use it for our purpose. An InputBox will ask the user to enter the value by displaying the InputBox.
Syntax
- Prompt: This is nothing but the message to the user through an input box.
- Title: What is the title of the input box?
- Default: What is the default value of the inputbox? This value appears in the typing area of the inputbox.
These three parameters are good enough in Excel. Ignore the other 4 optional parameters. To understand this syntax, look at the below screenshot.
How to Create InputBox in VBA?
Ok, lets straight jump to practicality. Follow the below steps to create your first ever inputbox.
Step 1: Go to VBE (Visual Basic Editor), and insert a new module.
Step 2: Double click on the inserted module and create a macro name.
Step 3: Start typing the word “InputBox” you will see related options.
Step 4: Select the inputbox and give space, and you will see the syntax of the inputbox.
Step 5: Give the prompt as “Please Enter Your Name.”
Step 6: Type the Title of the inputbox as “Personal Information.”
Step 7: Type the default value as “Type here.”
Step 8: We are done. Run this code and see your first input box.

4.6 (247 ratings) 3 Courses | 12 Hands-on Projects | 43+ Hours | Full Lifetime Access | Certificate of Completion
Store the Value of InputBox to Cells
Now we will go through the process of storing values in cells. Follow the below steps.
Step 1: Declaring the variable as Variant.
Code:
Sub InputBox_Example() Dim i As Variant End Sub
Step 2: For this variable, assign the value through the inputbox.
Code:
Sub InputBox_Example() Dim i As Variant i = InputBox("Please Enter Your Name", "Personal Information", "Type Here") End Sub
Note: Once the input box comes to the right of the equal sign, we need to enter the arguments or syntax in brackets like our regular formulas.
Step 3: Now, whatever the value typed in the input box, we need to store it in cell A1. So for this, write the code as Range (“A1”).Value = i
Code:
Sub InputBox_Example() Dim i As Variant i = InputBox("Please Enter Your Name", "Personal Information", "Type Here") Range("A1").Value = i End Sub
Ok, we are done. Let’s run this code now by pressing the F5 key, or you can also run the code manually, as shown in the below screenshot.
As soon as you run this code, we will see the inputbox.
Type the name and click on Ok.
As soon as you type the name and click on OK, you will see the inputbox value in cell A1.
Note: We can store any value from the inputbox if the variable is defined properly. In the above example, I have defined the variable as a Variant, which can hold all types of data.
For example, now I have changed the variable type to Date.
Now run the code and type other than the date.
Click on ok and see what the response is.
We got the error value as Type mismatch. Since we have declared the variable data type as DATE, we cannot store anything other than DATE with an inputbox.
Now enter the date and see what happens.
As soon as you type the date and then click on OK and see what the response is.
Since we have entered the correct value, we got the result in the cell.
Validation of Input from User
You know what we can actually allow users to enter only specific value i.e., allow the user to enter only text, only number, only logical values, etc.
To perform this task, we need to use the method Application.InputBox.
Let’s look at the syntax of the Application.InputBox.
- Prompt: This is nothing but the message to the user through an input box.
- Title: What is the title of the input box?
- Default: What is the default value of the input box? This value appears in the typing area of the input box.
- Left: What should be the x position of the input box in the current window?
- Top: What should be the y position of the inputbox in the current window?
To start this, inputbox declare variable and assign the value to a variable.
Now to assign value to start the word Application.
After the word Application, put a dot (.) and type Inputbox.
Select the input box and open the bracket.
As usual, enter Prompt, Title, and Default Value.
Now ignore left, top, help file, help context ID by typing 5 commas (,).
Here Type means what should be the input string. Below are the validations available.
So, accordingly, select your type. I have selected 1 as the parameter i.e., only numbers.
Now run the code and type of text value.
Click on OK and see what happens.
It says the number is not valid. So we can enter only numbers in this input box.
Things to Remember
- We need a variable to store the value given by the input box.
- If you are using InputBox without the Application method, you should be perfect about the variable data type.
- Use Variant data type, which can hold any kind of data type and store.
Recommended Articles
This has been a guide to VBA InputBox Function. Here we learn how to create a VBA InputBox and process the stored values along with some practical examples and a downloadable excel template. Below are some useful excel articles related to VBA –
- 3 Courses
- 12 Hands-on Projects
- 43+ Hours
- Full Lifetime Access
- Certificate of Completion