In VBA, the basic InputBox format is InputBox(prompt, title, default): it displays a dialog and returns the user’s response as text. In Excel, use Application.InputBox instead when you need to restrict the response to a number, a range, or another data type.
What “InputBox format” means in VBA
People asking about “VBA InputBox format” may mean the function’s syntax, the way text appears inside its dialog, or how to require a particular kind of answer. These are separate things. The standard VBA InputBox function lets you set a prompt, a title, a suggested default, and optionally the dialog’s position and help information. It does not provide a format setting that forces the user to enter a number, date, or range.
The standard function returns a String. That makes it useful for names, codes, comments, and other text, but it also means you must validate and convert the response yourself before using it as a number or date. An empty response is especially important: clicking Cancel and submitting an empty answer both return a zero-length string, so the function alone cannot tell those actions apart.
Excel also provides Application.InputBox, a method with an optional Type argument. It can validate the kind of entry at the dialog level and return a number, a range object, or another specified type. Choosing between these two options is the key to getting the “format” you need.
Standard InputBox syntax and arguments
The full syntax for the VBA function is:
InputBox(prompt[, title][, default][, xpos][, ypos][, helpfile, context])
The square brackets indicate optional arguments; they are notation, not characters to type. Arguments must be supplied in order. If you want to set a later argument while skipping one before it, use a comma placeholder or, more readably, named arguments in VBA.
promptis the message shown in the dialog. Supply a string expression that explains what information is needed.titleis the dialog’s title-bar text. If omitted, the application name is used.defaultis the initial text in the input field. It is optional and can make a sensible response easier to enter.xposandyposset the dialog’s horizontal and vertical position. They are measured in twips; omit them to let the dialog appear in its usual centered position.helpfileandcontextidentify a help file and a help topic context. They are normally unnecessary in a small Excel macro, and the two arguments are used together.
For ordinary prompts, keep the call simple. A clear prompt and an informative title are usually more useful than positioning or help arguments. Avoid using a default value that could be mistaken for a confirmed answer if the user simply presses Enter.
A basic text prompt
This example asks for a department name and places the response in a worksheet cell. The code treats an empty response as cancellation, which is a practical convention when a blank department name is not valid:
Sub AskForDepartment()
Dim department As String
' A blank result is treated as Cancel or no usable entry.
department = InputBox(Prompt:="Enter a department name:", _
Title:="Department", _
Default:="Sales")
If department = "" Then Exit Sub
ThisWorkbook.Worksheets("Sheet1").Range("A1").Value = department
End Sub
The response remains text, even if the user types digits. If the user enters 0042, a string variable can preserve the leading zeros; converting the response to a number would not. This matters for postal codes, employee IDs, and product codes that look numeric but are identifiers rather than quantities.
Before running a macro, save a copy of the workbook. VBA macros cannot be undone with Ctrl+Z, and a macro that writes to cells may overwrite existing data. Check the destination sheet and cell before using the example in a workbook that contains important information.
Getting a numeric response
With the standard function, test the response before converting it. VBA’s IsNumeric can identify text that VBA recognizes as numeric, but it does not enforce a business rule such as “must be a positive whole number.” Conversion and range checks are still needed. Numeric interpretation can also depend on regional settings, including the decimal separator.
For Excel-specific prompts, Application.InputBox can request a number directly with Type:=1. The method returns a Variant, and clicking Cancel returns the Boolean value False. Checking the Variant subtype distinguishes cancellation from a numeric zero:
Sub AskForQuantity()
Dim answer As Variant
' Type 1 requests a numeric response.
answer = Application.InputBox(Prompt:="Enter a quantity:", _
Title:="Quantity", _
Default:=1, _
Type:=1)
If VarType(answer) = vbBoolean Then
If answer = False Then Exit Sub
End If
If answer < 0 Then
MsgBox "Enter zero or a positive quantity."
Exit Sub
End If
MsgBox "Quantity entered: " & CStr(answer)
End Sub
Here, the prompt accepts zero or a positive number, but the code does not require a whole number. To require whole quantities, add a check such as comparing the value with its integer form, while considering whether negative values are allowed. The validation rule should match the data being collected rather than relying only on the dialog’s type filter.
Application.InputBox types and range selection
The Excel method’s Type argument determines what kind of response it accepts. Common values include 0 for a formula, 1 for a number, 2 for text, 4 for a logical value, and 8 for a range reference. Other documented values include 16 for an error value and 64 for an array. Some types can be combined by adding their numeric values; for example, 1 plus 2 permits a number or text response.
A range prompt is useful when a user should select cells rather than type an address. With Type:=8, the result is a Range object, so VBA code generally assigns it with Set. A cancelled range selection needs careful handling: assigning the result directly can raise an error, so use an error handler around the assignment or otherwise handle cancellation before using the object. Do not assume a range result is a text address.
The Type option belongs to Excel’s Application.InputBox, not the standard VBA InputBox. A call such as InputBox("Enter a number", Type:=1) does not become a typed Excel prompt; it uses a different function signature and will not work as intended. In Excel, qualify the method with Application. when you need its type feature.
Choosing the right prompt for the task
Use the standard InputBox when the answer is naturally text and you can handle validation in code. Choose Application.InputBox when Excel should constrain the response to a number or let the person select a worksheet range. Neither is a substitute for a UserForm when the interaction needs several fields, explanatory labels, buttons, or more control over validation.
- For a name, note, or identifier, use
InputBoxand store the result as text. - For a numeric value, use
Application.InputBoxwithType:=1, then enforce additional limits in VBA. - For selected cells, use
Application.InputBoxwithType:=8and handle cancellation and object assignment. - For several related inputs or a guided workflow, consider a UserForm instead of opening multiple dialogs in sequence.
Input boxes are modal: the user must respond to or dismiss the dialog before continuing with the macro. They are therefore suitable for quick, one-off questions, not for collecting a large set of information or presenting a complex data-entry process.
Common mistakes and practical fixes
A frequent mistake is treating an empty string as a reliable way to distinguish Cancel from a deliberately blank answer. With the standard function, those outcomes are indistinguishable. If blank is a valid answer but cancellation must be detected separately, use a different interface, such as a UserForm with separate buttons, or design the prompt so that blank is not a valid submission.
Another mistake is converting the response too early. For example, converting a product code to a number can remove leading zeros, and converting arbitrary text with CDbl can fail if the input is not numeric. Validate first, then convert. For dates, use date-aware validation and conversion rather than assuming every locale writes dates in the same order.
Keep prompts specific. “Enter value” gives little guidance; “Enter the invoice number, without spaces” tells the user what is expected. A default should be clearly appropriate, and a macro should not silently treat a default as verified input. When using Application.InputBox, explicitly set Type if the answer must be constrained; otherwise the method’s default allows a formula response.
Platform and macro considerations
This guidance concerns VBA in desktop Excel, including Excel for Microsoft 365 on Windows. VBA macros do not run in Excel for the web, so an InputBox macro must be run in a desktop version of Excel. Excel for Mac supports VBA, although the Visual Basic Editor and some surrounding controls can look different from Windows. The VBA function and Excel method are still the relevant distinction on either desktop platform.
Only run macros from workbooks and sources you trust. An InputBox itself is simply a dialog, but the code that runs after a response can edit cells, delete content, or perform other actions. Review what a macro does, save a backup copy first, and remember that Ctrl+Z cannot reverse its changes.
Frequently Asked Questions
How do I add a title to a VBA InputBox?
Supply a title as the second argument, for example InputBox("Enter a name:", "Customer"). The first argument is the prompt and the second is the title text.
How do I set a default value?
Use the third argument: InputBox("Enter a name:", "Customer", "Alex"). The text appears in the input field initially, and the user can replace it or accept it.
Can VBA InputBox accept only numbers?
The standard VBA InputBox returns text and has no type restriction argument. In Excel VBA, use Application.InputBox with Type:=1 to request a number, then apply any extra range or whole-number checks your task requires.
How can I tell if the user clicked Cancel?
With the standard InputBox, Cancel and submitting a blank response both return an empty string, so VBA cannot distinguish them through that return value alone. With Application.InputBox, Cancel returns Boolean False; check the returned Variant’s type so it is not confused with numeric zero.
Can InputBox let a user select a cell range?
Use Excel’s Application.InputBox with Type:=8. It returns a range reference, which should be assigned to an object variable with Set; include cancellation handling so the macro does not proceed without a valid selection.
Why does InputBox return text when I typed a number?
The standard VBA function always returns a String. Convert the result only after checking that it is valid for your purpose, or use Application.InputBox with a numeric type when working in Excel.
Related
- Parallel Box Plot in Excel: Create and Read One
- Open Excel Solver Add-in: Enable and Use It
- Open a Corrupted Excel File Online Safely
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- VBA Array Cheat Sheet: Syntax, Bounds, and ExamplesA VBA array stores multiple values under one variable name, and the essential pattern is to declare it,…
- VBA Sort Not Working? Fix Excel Sorting Macros with Clear RangesIf VBA sorting is not working, check that the macro is running, the sort range includes every column…
- VBA Loop Through a Range: Examples and Best PracticesTo loop through cells in Excel VBA, use For Each cell In worksheet.Range("A1:A10").Cells and place the action you…
- Excel Macro for Multiple-Selection Drop-Down ListsTo let a user choose several items from one Excel drop-down, add a VBA Worksheet_Change macro to the…
- Excel VBA: Open a Workbook in the Background, HiddenYes. In Excel VBA, open the file with Workbooks.Open, then set each window in the returned workbook’s Windows…
- Excel UserForm ComboBox: Load a List with VBATo load an Excel UserForm ComboBox from worksheet values, add a ComboBox to the form, then fill it…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.