VBA InputBox: Syntax, Types, and Real Examples

VBA & macros · Updated November 23, 2026

The VBA InputBox function displays a dialog box where the user can type a value, and it returns that value as a string. To get a specific data type (like a number or date) directly, you use InputBox with the Type argument, or you use the Application.InputBox method which offers more control over the returned type.

What Is the InputBox Function?

In VBA (Visual Basic for Applications), InputBox is a built-in function that creates a simple prompt with a text field, an OK button, and a Cancel button. It is part of the VBA language itself, so it works in Excel, Word, Access, and other Office applications. The function always returns a string, even if the user types a number. This is the most common source of confusion: if you ask for an age and the user types 25, you get the text "25", not the number 25.

The basic syntax is:

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

Only the prompt argument is required. The prompt is the message shown above the text field. The optional title appears in the dialog’s title bar. The optional default pre-fills the text box. The position arguments (xpos, ypos) control where the dialog appears on screen, and the last two arguments are for a custom help file.

The Type Argument: What It Does

The Type argument is the key to controlling what the user can enter and how VBA interprets the result. It is a numeric code that tells VBA what kind of data is expected. Here are the valid values:

  • 0 – Formula (only in Application.InputBox)
  • 1 – String (text)
  • 2 – Number
  • 4 – Boolean (True/False)
  • 8 – Cell reference (Range object)
  • 16 – Error value
  • 64 – Array of values

You can combine types by adding the numbers. For example, Type:=1 + 2 allows either text or a number. The most important distinction is between the plain VBA InputBox and the Excel-specific Application.InputBox. The plain VBA version does not support the Type argument at all. It always returns a string. The Application.InputBox method, which is part of the Excel object model, does support Type and returns the appropriate data type.

Using Application.InputBox for Real Types

If you need a number, a date, or a range, you should use Application.InputBox. Its syntax is very similar, but it adds the Type argument at the end:

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

For example, to get a numeric value from the user, you would write:

Dim userNumber As Double
userNumber = Application.InputBox("Enter a number:", "Numeric Input", 0, , , , , 2)

Here, Type:=2 forces the input to be a number. If the user types text, Excel shows an error message and asks again. If the user clicks Cancel, Application.InputBox returns False (a Boolean), not an empty string. This is a critical difference from the plain VBA version, which returns an empty string on Cancel.

To handle Cancel correctly, you must check for False before using the result:

Dim userNumber As Variant
userNumber = Application.InputBox("Enter a number:", "Numeric Input", , , , , , 2)
If userNumber = False Then
    MsgBox "You cancelled."
Else
    MsgBox "You entered " & userNumber
End If

How to Choose the Right Type

Selecting the correct Type depends on what you plan to do with the input. If you just need text, use Type:=1 or the plain VBA InputBox. If you need a number for calculations, use Type:=2. For a cell reference, use Type:=8 – this is extremely useful because it lets the user click a cell or drag a range directly in the worksheet, and VBA returns a Range object.

For dates, there is no dedicated date type. Instead, you can use Type:=2 (number) and then convert with CDate, or use Type:=1 and parse the string. A common workaround is to use Type:=2 and format the input as a date with Format. For Boolean values, Type:=4 gives a True/False dropdown in the input box, which is a neat way to get a yes/no answer without a custom form.

When you need multiple types, add the codes. For example, Type:=1 + 2 (which is 3) accepts text or numbers. However, be careful: if you combine types, the returned value is still a string unless you convert it. The Type argument only validates the input, it does not automatically convert the result to the chosen type (except for Type:=8 which returns a Range).

Common Problems and How to Avoid Them

One of the most frequent mistakes is using the plain VBA InputBox and then trying to treat the result as a number. For example, this code will cause a type mismatch error:

Dim age As Integer
age = InputBox("Enter age")  ' Error: string to integer

To fix it, you must convert with CInt, Val, or use Application.InputBox with Type:=2. Another common issue is forgetting that Application.InputBox returns False on Cancel. If you assign that directly to a numeric variable, you’ll get an error. Always use a Variant variable and check for False.

A third problem is that Type:=8 (range) returns a Range object, but if the user types a range address manually (like A1:B5) it works, but if they type an invalid reference, Excel shows an error. Also, if the user selects multiple areas, Application.InputBox with Type:=8 only returns the first area unless you set MultiSelect – but that’s a separate parameter not available in the standard method. For multiple selections, you need to use a loop with Selection.Areas after the input.

Finally, remember that macros cannot be undone with Ctrl+Z. Always save a copy of your workbook before running any macro that modifies data based on user input.

Comparing InputBox and InputBox Method

Here is a side-by-side comparison to clarify the two:

  • VBA InputBox (function): Always returns a string. No Type argument. Cancel returns an empty string (""). Works in all Office apps.
  • Application.InputBox (method): Returns the type you specify via Type. Cancel returns False. Only works in Excel. Supports Type:=8 for ranges and Type:=0 for formulas.

For most Excel automation, Application.InputBox is the better choice because it gives you type safety and range selection. The plain VBA version is simpler and works outside Excel, but you must handle string-to-number conversions manually.

Real-World Example: Collecting a Date

Suppose you want the user to enter a date and then highlight all rows in a list that match that date. Here’s how you would use Application.InputBox with a number type and convert it to a date:

Sub HighlightDateRows()
    Dim targetDate As Date
    Dim userInput As Variant
    Dim cell As Range
    
    ' Save a copy of the workbook before running macros
    ' This macro cannot be undone with Ctrl+Z
    
    userInput = Application.InputBox("Enter a date (e.g., 2025-03-15):", "Date Filter", Date, , , , , 2)
    If userInput = False Then Exit Sub  ' user cancelled
    
    targetDate = CDate(userInput)  ' convert number to date
    
    For Each cell In Range("A1:A100")
        If IsDate(cell.Value) Then
            If CDate(cell.Value) = targetDate Then
                cell.EntireRow.Interior.Color = vbYellow
            End If
        End If
    Next cell
End Sub

This example shows the pattern: use a Variant to capture the result, check for False, then convert. The Type:=2 ensures the user enters a number (dates are stored as numbers in Excel), and CDate converts that number to a proper date value.

Frequently Asked Questions

Q: Why does my InputBox return a string even when I type a number?
Because the plain VBA InputBox function always returns a string. To get a number, use Application.InputBox with Type:=2, or convert the result with CInt, CLng, or Val.

Q: How do I check if the user clicked Cancel?
With Application.InputBox, check if the result equals False. With the plain VBA InputBox, check if the result is an empty string ("").

Q: Can I let the user select a range with InputBox?
Yes, use Application.InputBox with Type:=8. The user can click and drag to select cells, and VBA returns a Range object.

Q: What does Type:=0 do?
Type:=0 is only valid in Application.InputBox. It expects a formula as text, like =SUM(A1:A5). It returns the formula as a string, not the calculated result.

Q: Is there a way to limit input to a specific length?
No, the built-in InputBox does not support length limits. You would need to validate the result after the user clicks OK, or create a custom UserForm with a TextBox and a MaxLength property.

Q: Does InputBox work in Excel for Mac?
Yes, both the VBA function and Application.InputBox work in Excel for Mac. The syntax is the same. However, some dialog appearances differ slightly, and keyboard shortcuts differ (e.g., Command+Z instead of Ctrl+Z for undo).

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.