VBA InputBox

Publication Date :

Blog Author :

Download FREE VBA InputBox Excel Template and Follow Along!
VBA InputBox Excel Template.xlsx

Table Of Contents

arrow

Excel VBA InputBox

VBA InputBox is an inbuilt function used to get a value from the user. This function has two major arguments: the heading for the input box and the question for the input box. The InputBox function can store only the data types input that its variable can hold.

Often in Excel, we use the data already in the Excel sheet. However, sometimes we need some input data from users as well. Especially in VBA, input from the user is required frequently.

Using 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.

VBA InputBox.jpg
You are free to use this image on your website, templates, etc.. Please provide us with an attribution link

Syntax

InputBox Syntax
  • Prompt: This is nothing but the message to the user through an InputBox.
  • Title: What is the title of the InputBox?
  • 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 four optional parameters. To understand this syntax, look at the below screenshot.

Excel VBA Input Box11

How to Create InputBox in VBA?

Let us jump straight 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 1 - Go to VBE (Visual Basic Editor)

Step 2: Double click on the inserted module and create a macro name.

Step 2 - Double click on the inserted module

Step 3: Start typing the word "InputBox" you will see related options.

Step 3 - Start typing the word InputBox

Step 4: Select the InputBox and give space, and you will see the syntax of the InputBox.

Step 4 - Select the InptBox

Step 5: Give the prompt "Please Enter Your Name."

Step 5 - Give the prompt

Step 6: Type the title of the InputBox as "Personal Information."

Step 6 - Type the title of the InputBox

Step 7: Type the default value as "Type here."

VBA Input Box8

Step 8: We have completed it now. Run this code and see your first InputBox.

VBA Input Box9
VBA Input Box10

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 InputBox 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

We have completed it now. Let us run this code now by pressing the F5 key, or you can also run the code manually, as shown in the below screenshot.

VBA Input Box12

As soon as you run this code, we will see the inputbox.

VBA Input Box13

Type the name and click on "OK."

VBA Input Box14

As soon as you type the name and click "OK," you will see the InputBox value in cell A1.

VBA Input Box15

Note: We can store any value from the inputbox if the variable is defined properly. In the above example, we have defined the variable as a Variant that can hold all data types.

For example, now we have changed the variable type to Date.

VBA Input Box16

Now, run the code and type other than the date.

VBA Input Box17

Click on "OK" and see what the response is.

Excel VBA Input Box18

We got the error value as Type mismatch. Since we have declared the variable data type DATE, we cannot store anything other than DATE with an InputBox.

Now, enter the date and see what happens.

Excel VBA Input Box19

As soon as you type the date, click on "OK" and see what the response is.

Excel VBA Input Box20

Since we entered the correct value, we got the result in the cell.

Validation of Input from User

You know what? We can allow users to enter only specific values, i.e., allow the user to enter only text, only numbers, only logical values, etc.

To perform this task, we need to use the method Application.InputBox.

Let us look at the syntax of the Application.InputBox.

VBA Input Box21
  • Prompt: This is nothing but the message to the user through an InputBox.
  • Title: What is the title of the InputBox?
  • Default: What is the default value of the InputBox? This value appears in the typing area of the InputBox.
  • Left: What should be the x position of the InputBox in the current window?
  • Top: What should be the y position of the InputBox in the current window?

For starting, the InputBox declares a variable and assigns the value to a variable.

Validation of Input22

Now, assign a value to start the word Application.

Validation of Input23

After the word "Application," put a dot (.) and type "InputBox."

Validation of Input24

Then, select the input box and open the bracket.

Validation of Input25

As usual, enter Prompt, Title, and Default Value.

Validation of Input26

Now ignore left, top, help file, and help context ID by typing 5 commas (,).

Validation of Input27

Here, "Type" means what should be the input string. Below are the validations available.

Validation of Input28

So, accordingly, select your type. We have selected 1 as the parameter, i.e., only numbers.

Validation of Input29

Now, run the code and type of text value.

Validation of Input30

Click on "OK" and see what happens.

Validation of Input31

It says the number is not valid. So, we can enter only numbers in this InputBox.

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 data type and store it.