InputBox in VBA: Syntax, Usage & Examples

VBA & macros · Updated November 23, 2026

An InputBox in VBA is a built-in dialog box that prompts the user for a single piece of text input, returning that value as a string when the user clicks OK. The basic syntax is InputBox(prompt[, title][, default][, xpos][, ypos][, helpfile, context]), and it is the simplest way to gather one value from a user without building a custom UserForm.

What Is the VBA InputBox and When to Use It

The InputBox function is part of the VBA language and works in any Microsoft Office application (Excel, Word, Access, PowerPoint). It displays a small dialog with a text field, an OK button, and a Cancel button. When the user types something and clicks OK, the function returns the entered text as a string. If the user clicks Cancel, it returns an empty string (“”).

Use InputBox when you need a quick, simple answer from the user—like a name, a number, or a file path—and you don’t need a complex form with multiple fields. For example, you might ask for a starting date or a folder name. For more complex input (multiple fields, validation, or dropdowns), a UserForm is better.

InputBox Syntax and Arguments Explained

The full syntax is:

InputBox(prompt, [title], [default], [xpos], [ypos], [helpfile, context])
  • prompt (required): The message displayed in the dialog. Can be up to 1024 characters, and you can include line breaks using Chr(10) or vbCrLf.
  • title (optional): The text in the title bar. If omitted, the application name is used.
  • default (optional): A string displayed in the input field when the dialog opens. If omitted, the field is empty.
  • xpos and ypos (optional): The horizontal and vertical position of the dialog in twips (1/1440 inch) from the top-left of the screen. Rarely used.
  • helpfile and context (optional): Add a Help button that links to a specific help topic. If you use one, you must use both.

Example with all optional arguments:

Dim userName As String
userName = InputBox("Enter your name:", "User Login", "John Doe")
If userName = "" Then
    MsgBox "You cancelled."
Else
    MsgBox "Hello, " & userName
End If

How to Use InputBox in Excel VBA: Step-by-Step

To use InputBox in Excel VBA, you open the Visual Basic Editor (Alt+F11), insert a new module, and write a macro. Here’s a simple example that asks for a number and writes it to cell A1:

Sub GetNumber()
    Dim userInput As String
    Dim num As Double
    userInput = InputBox("Enter a number:", "Data Entry")
    If userInput = "" Then Exit Sub  ' user cancelled
    num = CDbl(userInput)  ' convert to number
    Range("A1").Value = num
End Sub

Remember to save a copy of your workbook before running any macro, because macros cannot be undone with Ctrl+Z.

To run the macro, press F5 while in the editor, or assign it to a button on the sheet. The InputBox will appear, and the value entered will be placed in cell A1.

InputBox vs. Application.InputBox: Key Differences

Excel also has a separate object, Application.InputBox, which is more powerful and Excel-specific. The main differences:

  • Return type: The VBA InputBox always returns a string. Application.InputBox can return a number, a date, a range, or a formula, depending on the Type argument.
  • Type parameter: Application.InputBox has a Type argument (0 for formula, 1 for number, 2 for text, 8 for range, etc.). This allows built-in validation, like rejecting non-numeric input.
  • Cancel behavior: With Application.InputBox, if the user clicks Cancel, the function returns False (a Boolean), not an empty string. This makes it easier to detect cancellation.

Example using Application.InputBox to get a range:

Dim rng As Range
Set rng = Application.InputBox("Select a range:", Type:=8)
If rng Is Nothing Then Exit Sub  ' cancelled
rng.Interior.Color = vbYellow

Handling User Input: Numbers, Dates, and Validation

Because the standard InputBox returns a string, you must convert it to the desired data type. Use Val() or CDbl() for numbers, CDate() for dates, and CBool() for booleans. Always check for cancellation and validate the input to avoid runtime errors.

Example with validation:

Sub GetValidNumber()
    Dim inputStr As String
    Dim num As Double
    Do
        inputStr = InputBox("Enter a positive number:", "Validation")
        If inputStr = "" Then Exit Sub  ' cancelled
        If IsNumeric(inputStr) And CDbl(inputStr) > 0 Then
            num = CDbl(inputStr)
            Exit Do
        Else
            MsgBox "Invalid input. Try again."
        End If
    Loop
    MsgBox "You entered: " & num
End Sub

This loop continues until the user enters a positive number or cancels. The IsNumeric function checks if the string can be interpreted as a number.

Common Problems and How to Avoid Them

  • Cancel returns empty string: If the user clicks Cancel, you get “”. Always check for this and exit gracefully.
  • Type mismatch errors: Converting a non-numeric string to a number causes an error. Use IsNumeric before conversion.
  • InputBox disappears behind Excel? This can happen if the application is not active. Use AppActivate or set the window to front, but it’s rare.
  • Line breaks in prompt: Use vbCrLf to create multi-line prompts.
  • Long prompts: The prompt is limited to 1024 characters. Keep it concise.

Frequently Asked Questions

How do I make an InputBox accept only numbers?

Use Application.InputBox with Type:=1 (number). That forces the user to enter a number, and if they type text, Excel shows an error. Example: Application.InputBox("Enter a number", Type:=1).

What does InputBox return if the user clicks Cancel?

The standard VBA InputBox returns an empty string (“”). Application.InputBox returns False (a Boolean) if the user cancels. Always check for these values.

Can I use InputBox in Word or PowerPoint?

Yes, the VBA InputBox function works in all Office applications. The syntax is identical, but the application-specific methods like Application.InputBox are only available in Excel.

How do I set a default value in an InputBox?

Pass the default value as the third argument: InputBox("Enter name", "Prompt", "Default Name"). The user can overwrite it.

Is there a way to make the InputBox always on top?

No built-in parameter. You can use Windows API calls to set the window position, but it’s complex. For critical prompts, consider a UserForm.

Why does my InputBox return a number as text?

Because the standard InputBox always returns a string. Use Val() or CDbl() to convert it to a numeric type before doing arithmetic.

Alternatives to InputBox: When to Use a UserForm

If you need more than one input, or you want to validate input in real-time, or you need a dropdown list, a UserForm is the better choice. UserForms are created in the VBA editor and can contain text boxes, combo boxes, list boxes, and command buttons. They give you full control over the layout and behavior. For a single, simple input, InputBox is faster and requires less code.

Another alternative is using a simple cell input: you can ask the user to type a value into a specific cell and then read that cell. This avoids popups altogether, but it’s less user-friendly.

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.