answer = InputBox("What is your name?", "Enter Name") shows a box with a prompt, a title and a text field, and returns what the user typed as text. A third argument sets a default entry: InputBox("Region?", "Sales", "East").
Check for Cancel before using the answer. Cancel returns an empty string, the same as clicking OK with nothing typed:
Dim answer As String
answer = InputBox("Enter your name", "Name")
If answer = "" Then Exit Sub
Range("A1").Value = answer
For a number, use Application.InputBox("Enter the target", Type:=1). Excel rejects anything that isn’t a number before the box closes, and Cancel returns False, so store the result in a Variant. Type:=8 lets the user select cells and returns a range, which you assign with Set; Cancel then raises an error, so put On Error Resume Next before that line and test If rng Is Nothing Then Exit Sub after it.
To add each answer to the next empty row, use Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).Value = answer. Counting entries with CountA gives the wrong row as soon as the column has a gap.
As the name suggests, Input Box is a function that shows an input box to the user and collects a response.
Look at the example below, where we have an input box that asks for your name, with a text box to enter it. If you look at the title bar, which is just like the one in a message box, we have a title that says “Enter Name”, and the message shows “What’s your name?”.

Below is the syntax of the VBA Input Box:

InputBox(Prompt, [Title], [Default], [XPos], [YPos], [HelpFile], [Context])
- Prompt: In the prompt, you can specify the message you want to show in the input box. You can use a maximum of about 1024 characters in it.
- [Title]: A string that shows in the title bar of the input box. If you skip the title, the application name will be placed in the title bar.
- [Default]: The text that you want to show to the user in the text box by default. If you skip it, the text box is empty.
- [XPos]: A number (position on the X-axis, in twips) that represents the distance of the input box from the left side of the screen. If left blank, the input box will be horizontally centered.
- [YPos]: A number (position on the Y-axis, in twips) that represents the distance of the input box from the top of the screen. If left blank, the input box will be placed about one-third of the way down the screen.
- [HelpFile]: To specify a help file for the user. The user can access that help file using the help button. If you use it, you must also specify the context.
- [context]: A numeric expression that identifies the help context number of the help topic. If the context is provided, a help file must also be provided.
All the arguments that are in square brackets are optional. If you skip any of those, you can still use the input box function, and in most cases, you won’t need to use more than the first three arguments. The input box returns whatever the user types as text (a string).
How to Use Input Box in a VBA Code
- Type “InputBox”, enter a space, and you will get a tooltip for the arguments you need to define.
- Specify the “Prompt”, the message that you want to show to the user.
- Define a title for the input box; otherwise, it will show the default title.
- Mention the text that you want to have in the text box by default.

When you run this code, it will show you an input box just like the following:

Example to Use VBA Input Box in Excel
You might be wondering how you can use an input box in the real world. In the following example, I have shared a simple but powerful example.
Sub vba_input_box() 'variable to store the count of rows. Dim iRow As Long 'get the count cells that are filled iRow = WorksheetFunction.CountA(Range("A:A")) 'get to the next blank cell in column A Cells(iRow + 1, 1).Select 'enter the value from the input box into the active cell ActiveCell = InputBox("What is your name?", "Enter Name") End Sub
The above code enters the names in column A one after another. It counts the cells that are filled in the column and moves to the next blank cell. After that, it takes the value from the input box and enters it there.

In the code, if all the names go one after another in column A without any blank cells in between, CountA gives you the right row. If there’s a blank cell in the column, the code can write over an existing name, so in that case, find the last row with End(xlUp) instead.
Handle the Cancel Button in an Input Box
When the user clicks Cancel (or clicks OK without typing anything), InputBox returns an empty string (“”). If you don’t check for this, the code above enters an empty value in the cell. So, store the response in a variable first and check it before you use it:
Sub input_box_cancel()
Dim myName As String
myName = InputBox("What is your name?", "Enter Name")
If myName = "" Then
MsgBox "You didn't enter a name."
Exit Sub
End If
Range("A1").Value = myName
End Sub
The text box in an input box takes up to 255 characters, and the value you get back is cut to 254 characters.
Get a Number from an Input Box
InputBox always returns text, even when the user types a number. If you need a number, check the value with IsNumeric and then convert it with CDbl (or CLng for whole numbers):
Sub input_box_number()
Dim response As String
response = InputBox("Enter the quantity:", "Quantity")
If IsNumeric(response) Then
Range("B1").Value = CDbl(response)
Else
MsgBox "Please enter a number."
End If
End Sub
Use Application.InputBox to Control the Input Type
Excel also has its own input box, Application.InputBox. It looks almost the same, but it has a Type argument that tells Excel what kind of input to accept. If the user enters the wrong type, Excel shows a message and asks again. Here are the most useful values for Type:
- 1: A number.
- 2: Text (this is the default).
- 4: A logical value (TRUE or FALSE).
- 8: A cell reference, which you get back as a range.
With Application.InputBox, Cancel returns False instead of an empty string. The code below only accepts a number, and it checks for Cancel with VarType, because a simple qty = False check is also true when the user enters 0:
Sub application_input_box()
Dim qty As Variant
qty = Application.InputBox("Enter the quantity:", "Quantity", Type:=1)
If VarType(qty) = vbBoolean Then Exit Sub
Range("B1").Value = qty
End Sub
Let the User Select a Range with an Input Box
With Type 8, the user can select cells on the sheet with the mouse while the input box is open. You need to use Set, because the result is a range. And if the user clicks Cancel, Set gives an error, so the code turns off error messages for that one line:
Sub input_box_select_range()
Dim rng As Range
On Error Resume Next
Set rng = Application.InputBox("Select the cells to color:", "Select Range", Type:=8)
On Error GoTo 0
If rng Is Nothing Then Exit Sub
rng.Interior.Color = vbYellow
End Sub
Frequently Asked Questions
How do I show the prompt on more than one line?
Join the lines with vbNewLine (or vbCrLf), for example InputBox(“Enter your name.” & vbNewLine & “Use first name only.”, “Enter Name”).
Can I hide the text like a password in an input box?
No. Neither InputBox nor Application.InputBox can mask the text. For that, you need a UserForm with a text box that uses the PasswordChar property.
What is the difference between InputBox and Application.InputBox?
InputBox is a VBA function that always returns text and returns an empty string on Cancel. Application.InputBox is an Excel method that can check the type of input (number, text, range and more) and returns False on Cancel.
Written by
Puneet Gogia
Microsoft MVP — Excel · Founder, Excel Champs
Twelve years of teaching Excel, and a decade before that using it as a data analyst. Every tutorial here is written from a file I actually built.