Excel VBA InputBox Function: Syntax and Examples

VBA & macros · Updated November 21, 2026

The Excel VBA InputBox function displays a dialog that asks a user for a response and returns that response as a string. Call it from a VBA procedure, for example userName = InputBox("Enter your name:", "Name"); use Excel’s separate Application.InputBox method when you need Excel to return a number, a cell reference, or another specified data type.

What the VBA InputBox function does

The VBA function is a simple way for a macro to collect one piece of information while it runs. A prompt appears in a dialog box, the user types a response, and the procedure continues after the user clicks OK or Cancel. Typical uses include asking for a name, a search term, a short label, or a value that the macro will check before using.

InputBox is VBA code, not a worksheet function. You cannot enter it as a formula in a cell and expect a dialog to appear. Put it in a procedure in the Visual Basic Editor, then run that procedure in the desktop version of Excel. Excel for the web does not run VBA macros; use desktop Excel for these examples.

Syntax and available arguments

The VBA function has this syntax:

InputBox(prompt, [title], [default], [xpos], [ypos], [helpfile], [context])

Only prompt is required. The remaining arguments are optional and are supplied in the order shown. In ordinary macros, the first three arguments are usually all you need:

  • prompt is the message shown in the dialog. Make it clear what the user should enter.
  • title is the dialog’s title-bar text. If omitted, VBA uses the application name.
  • default is text initially displayed in the input field. It is a suggested response, not a validation rule.
  • xpos and ypos set the dialog’s horizontal and vertical position, measured in twips. If omitted, the dialog is centered on the relevant screen.
  • helpfile and context identify a Help file and a Help topic context. These are specialized arguments and can be left out for typical prompts.

Because the arguments are positional, omitting an earlier optional argument while supplying a later one can be awkward. In that case, use named arguments in VBA, such as InputBox(prompt:="Enter a code", title:="Lookup"). Named arguments make a call easier to read and reduce the risk of putting a value in the wrong position.

A basic macro example

Before running any macro, save a copy of the workbook. VBA actions cannot be reversed with Ctrl+Z, and a later part of a macro might change workbook data even if the input dialog itself does not. This simple example asks for a project name and reports the result:

Sub AskForProjectName()
    Dim projectName As String

    ' Ask for one text response.
    projectName = InputBox("Enter the project name:", "Project")

    If Len(projectName) = 0 Then
        MsgBox "No project name was entered."
    Else
        MsgBox "Project: " & projectName
    End If
End Sub

To add and run the procedure, open the Visual Basic Editor in desktop Excel, insert a standard module, and paste the code into it. You can run the procedure from the editor or from Excel’s macro list. The exact keyboard shortcut for opening the editor can depend on the computer’s keyboard settings; on some Mac keyboards, using a function key also requires the Fn key. Macro security settings may require you to allow a trusted workbook’s macros before they can run.

Return value, Cancel, and empty responses

The VBA InputBox function returns a string. If a person types digits, the returned value is still text until the macro converts or interprets it. For instance, a response of 25 is not automatically a numeric variable just because it looks like a number.

A key limitation is that clicking Cancel returns a zero-length string, just as clicking OK with the input field left blank does. The function does not provide a separate Cancel flag. Therefore, checking Len(response) = 0 is useful when both outcomes should mean “no value,” but it cannot tell you which button the user clicked. If that distinction matters, consider Application.InputBox, whose Cancel behavior can be detected when it returns Boolean False.

The optional default text can make the prompt faster to use, but it also means the field may not be blank when the dialog opens. For example, if the default is Current, clicking OK without editing it returns that word. Do not treat a default as proof that the user deliberately reviewed or confirmed the value.

Checking and converting what the user typed

Since the function returns text, validate it before using it in calculations, file operations, or decisions. For a numeric entry, IsNumeric can check whether VBA recognizes the response as a number, and CDbl can convert an accepted response to a Double. Numeric interpretation can depend on regional settings, including the decimal separator, so prompts should show an example that matches the workbook’s expected format.

Sub AskForQuantity()
    Dim response As String
    Dim quantity As Double

    ' Request a value, then check the returned text.
    response = InputBox("Enter a quantity, such as 12.5:", "Quantity")

    If Len(response) = 0 Then Exit Sub

    If Not IsNumeric(response) Then
        MsgBox "Enter a valid numeric quantity."
        Exit Sub
    End If

    quantity = CDbl(response)
    MsgBox "Quantity entered: " & CStr(quantity)
End Sub

This check rejects text VBA does not recognize as numeric, but it is not a complete business rule. If only whole numbers or values within a particular range are acceptable, add those checks explicitly. For example, a quantity may need to be greater than zero, and an identifier that begins with zero may need to remain text rather than be converted to a number.

InputBox versus Application.InputBox

These similarly named options are different. InputBox is the VBA function and always returns a string. Application.InputBox is an Excel method with a Type argument that lets you request a particular kind of response. It is often the better choice when the macro needs Excel to validate an entry’s basic type before proceeding.

For example, with Type:=1, Excel requests a number. The method returns a Variant, and Cancel returns Boolean False. Checking the Variant’s type first avoids confusing Cancel with a legitimate numeric zero:

Sub AskForScore()
    Dim response As Variant
    Dim score As Double

    ' Request a number; Cancel returns Boolean False.
    response = Application.InputBox( _
        Prompt:="Enter a score:", _
        Title:="Score", _
        Type:=1)

    If VarType(response) = vbBoolean Then
        If response = False Then Exit Sub
    End If

    score = CDbl(response)
    MsgBox "Score entered: " & CStr(score)
End Sub

Other documented Type values include 2 for text, 4 for a logical value, 8 for a range reference, 16 for an error value, and 64 for an array. Type 0 requests a formula. When requesting a range with type 8, VBA code must handle the returned object reference appropriately; it is not simply a text address. Choose the method based on the kind of response you need, rather than assuming the two input boxes behave identically.

Common problems and practical safeguards

  • Blank and Cancel look the same: With the VBA function, both produce an empty string. Use the Excel method if the macro must distinguish cancellation from an accepted value.
  • A number is treated as text: Convert it deliberately after checking it. Do not rely on VBA to infer the variable type from what the person typed.
  • Invalid input causes a later error: Check length, allowed characters, numeric range, or other requirements before using the response. A prompt alone does not enforce rules.
  • The dialog seems not to appear: Confirm that the procedure actually ran and that the dialog is not behind another window. Input boxes are modal, so the user must respond before the macro continues.
  • The macro will not run in a browser: VBA execution requires desktop Excel. Excel for the web can open many macro-enabled workbooks, but it cannot run their VBA code.
  • Macros are blocked: Only enable macros in files from sources you trust. A dialog that collects input is not inherently dangerous, but the surrounding macro can perform other actions, including changing workbook content.

Keep prompts specific and explain the expected format. If the response will be used to locate a worksheet, file, or record, check that the target exists before acting. Save a copy first, especially when developing code that changes cells or workbook structure, because Ctrl+Z does not undo VBA changes.

When a different input method is better

An input box is convenient for a quick, one-off response, but it is not always the best interface. If a user must enter several related values, a worksheet form with labeled cells can show more context and allow review before a macro runs. If a process needs dropdowns, required fields, or a more structured dialog, a VBA UserForm may be a better fit, although it takes more setup.

For simple prompts, use the VBA function when text is enough and treating Cancel like a blank response is acceptable. Use Application.InputBox when you want Excel to request a specific value type or need its distinct Cancel result. Use a worksheet-based interface or UserForm when users need to enter multiple fields, see instructions alongside them, or correct several values together.

Frequently Asked Questions

What does InputBox return in Excel VBA?

The VBA InputBox function returns a String containing the user’s response. Clicking Cancel returns a zero-length string, which is indistinguishable from clicking OK without entering anything.

How do I set a title and default value?

Pass them as the second and third arguments: InputBox("Enter a department:", "Department", "Sales"). The title appears in the dialog’s title bar, and the default appears in the input field.

Can InputBox accept only numbers?

The VBA function returns text, so check the response with IsNumeric and convert it if valid. For Excel’s built-in type checking, use Application.InputBox with Type:=1, then handle its Boolean False Cancel result.

Can I use InputBox in a worksheet cell?

No. It is a VBA function, not an Excel worksheet formula. Call it from a VBA procedure in desktop Excel; it will not display a dialog when entered into a cell or run in Excel for the web.

What is the difference between InputBox and Application.InputBox?

InputBox is a VBA function that returns text. Application.InputBox is an Excel method that can request data types such as a number or range and returns a Variant, with a distinct Boolean False result when the user cancels.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.