The Application.InputBox method in Excel VBA is used to prompt a user for a specific piece of information, such as a number, a string of text, or a cell range, and return that value to the macro for further processing. Unlike the standard VBA InputBox function, the Application version allows you to enforce a specific data type (Type), ensuring that the user cannot enter a word when the macro expects a number or a range of cells, which significantly reduces runtime errors.

What You'll Need

  • Microsoft Excel installed on a Windows or Mac computer.
  • The Developer tab enabled in the Excel Ribbon.
  • Basic familiarity with opening the Visual Basic Editor (VBE) via Alt + F11.
  • A standard module created within the VBE to house your code.

Step-by-Step

  1. Open the Visual Basic Editor: Press Alt + F11 on your keyboard to launch the VBE window. If the Developer tab is enabled, you can also click Developer > Visual Basic.
  2. Insert a Module: In the project explorer on the left, right-click your workbook name, select Insert, and then click Module. This provides a clean slate to write your procedure.
  3. Declare Your Variables: Before calling the InputBox, declare a variable to hold the result. Because Application.InputBox can return different types of data, use a Variant for general purposes or a specific object like Range if you are requesting a cell selection. For example: Dim userResponse As Variant or Dim selectedRange As Range.
  4. Write the InputBox Syntax: Start typing your procedure. The basic syntax is: Application.InputBox(Prompt, Title, Default, Left, Top, HelpFile, HelpContextID, Type). Most users only need the Prompt, Title, and Type.
  5. Define the Prompt and Title: The Prompt is the message the user sees (e.g., "Please enter your age"). The Title is the text that appears in the window's title bar (e.g., "Data Entry"). Use double quotes around these strings.
  6. Set the Type Parameter: This is the most critical part of Application.InputBox. The Type argument determines what the user is allowed to enter:
    • 0: A formula.
    • 1: A number.
    • 2: Text (string).
    • 4: A logical value (True/False).
    • 8: A cell reference or range.
  7. Handle the Cancel Button: When a user clicks "Cancel", the method returns False. If you have declared your variable as a Range, it will return Nothing. You must include an If statement to check for this, otherwise, your code will crash when it tries to process a cancelled input.
  8. Execute the Code: Press F5 or click the Run button to test the prompt.

Common Mistakes

One of the most frequent errors is confusing Application.InputBox with the built-in VBA InputBox function. The built-in version is simpler but only returns a string. If you use the built-in version and ask for a number, VBA will return that number as text, which can lead to calculation errors unless you explicitly convert it using CInt or CDbl. The Application version handles this conversion for you via the Type parameter.

Another common mistake is neglecting the False return value. If you set Type:=1 (number) and the user clicks Cancel, the variable receives the boolean value False. If your code then tries to use that variable in a mathematical equation, Excel will throw a "Type Mismatch" error. Always validate that the response is not False before proceeding.

Users often forget that when Type:=8 is used, the result is a Range object, not a string containing the address of the range. If you try to assign the result of a Type 8 InputBox to a String variable, the code will fail. You must assign it to a variable declared As Range.

Tips

To make your prompts more professional, use line breaks in your prompt string. You can achieve this by using vbCrLf or vbNewLine. For example: "Please enter the date" & vbCrLf & "Format: MM/DD/YYYY". This prevents the input box from becoming too wide and cutting off text on smaller screens.

When requesting a range (Type 8), you can pre-select a range for the user by setting the Default argument to the address of a range, such as "A1:B10". This guides the user toward the expected area of the spreadsheet.

For maximum robustness, wrap your InputBox in a Do While loop. This allows you to repeatedly prompt the user until they either provide a valid input or click Cancel, preventing the macro from ending prematurely due to a single typo.

Troubleshooting

If your code throws a "Type Mismatch'" error immediately after the user clicks OK, check your variable declarations. If you used Type:=8, ensure your variable is Dim myVar As Range. If you used Type:=1, ensure your variable is Dim myVar As Double or Variant.

If the InputBox does not appear, check if another modal dialog box is open in Excel. VBA cannot launch a new InputBox if another window (like a Find/Replace dialog or a different UserForm) is currently blocking the interface. Close all other dialogs and try again.

If the user is able to enter text when you specifically requested a number (Type 1), ensure you are actually using Application.InputBox and not the standard InputBox. The standard version ignores the Type argument entirely because it does not support it.

If the macro crashes when the user clicks Cancel, add a check immediately following the InputBox line. For numeric or text inputs, use If userResponse = False Then Exit Sub. For range inputs, use If selectedRange Is Nothing Then Exit Sub.

Frequently Asked Questions

Can I limit the number of characters a user enters?

No, Application.InputBox does not have a built-in character limit. To achieve this, you must capture the input as a string and then use the Len() function to check the length. If it exceeds your limit, you can trigger a message box and loop back to the input prompt.

How do I force the user to enter a date?

There is no specific "Date" type in Application.InputBox. You should use Type:=2 (text) and then use the IsDate() function to verify if the entry is a valid date. If IsDate(userResponse) returns False, prompt the user again.

Can I change the color or font of the InputBox?

No, the Application.InputBox is a system-standard dialog and its appearance cannot be customized. If you need a custom look, you must create a UserForm in the VBA editor, add a TextBox and a Label, and design it to look like an input box.

What happens if the user enters a formula when Type is set to 1?

If Type:=1 is specified, Excel will attempt to evaluate the formula. If the formula results in a number, it will be accepted. If the formula results in an error or text, Excel will automatically display a warning message to the user stating that the value does not match the required type and will prompt them to try again before returning control to your code.

Is there a way to hide the input text as the user types?

No, Application.InputBox always shows the text in plain view. For passwords or sensitive data where characters must be masked (e.g., replaced by asterisks), you must use a custom UserForm and set the PasswordChar property of the TextBox.