Use VBA's InputBox function to display a pop-up dialog that asks the user to type a value, then store or act on that typed value. The shortest working version is a single line placed inside a Sub in a standard module: Dim answer As String followed by answer = InputBox("Enter your name"). When you run the macro, Excel (or Word, Access, PowerPoint, Outlook) shows a small modal box with a text field, an OK button and a Cancel button, the user types something, presses OK, and the text lands in the variable. Everything else in this guide is refinement: validation, handling Cancel, converting text to numbers or dates, and writing the result into a worksheet cell.
What You'll Need
- A desktop installation of Excel (or Word, Access, PowerPoint, or Outlook) with macros enabled. The InputBox function is part of the VBA library in every one of these applications, so the technique is the same everywhere.
- The Developer tab visible, so you can get to the Visual Basic Editor. If it is not showing, right-click the ribbon, choose Customize the Ribbon, and tick Developer in the right-hand list. On Mac, the menu path is Tools > Macro > Visual Basic Editor instead, and the Developer tab is enabled from Excel settings.
- A workbook saved as .xlsm (macro-enabled). A plain .xlsx file cannot keep macro code.
- Basic familiarity with variables and the editor window. You do not need to know VBA well; each line is explained below.
Step-by-Step
- Open the workbook you want the prompt to live in, then press Alt+F11 (Windows) to open the Visual Basic Editor. On Mac, use Tools > Macro > Visual Basic Editor. Menu labels can differ slightly between versions, devices and regions, so if you cannot find an item, look for the same idea under a nearby menu.
- In the Project Explorer on the left, right-click your workbook's name and choose Insert > Module. A blank code window appears. Using a standard module rather than a sheet or ThisWorkbook module keeps the macro easy to find and run from the Macros list.
- Type the skeleton of the procedure. VBA inserts
Option Explicitif you have that setting on, which is recommended: it forces every variable to be declared and catches typos. Your starting code looks like this:Sub AskForName() Dim userName As String userName = InputBox("Please type your full name:") MsgBox "Hello, " & userName End Sub - Read what each argument does.
InputBox(Prompt, Title, Default, XPos, YPos, HelpFile, Context)is the full signature. The first argument is mandatory and is the text shown above the text field. The second is the caption shown in the box's title bar. The third pre-fills the text field, which is useful when you want to suggest an existing value the user can edit (for example a current quantity). Arguments you skip are supplied automatically, so most macros only use the first two. - Run the macro to test it. Press F5 while the cursor is inside the procedure, or close the editor, press Alt+F8, select AskForName and click Run. The box appears, you type, you click OK, and a message box echoes the text back to you.
- Handle Cancel properly, because this is where most first attempts break. Clicking Cancel returns an empty string, and clicking the close X behaves the same in many cases. If your next line writes to a cell or does arithmetic, an empty or missing value can produce nonsense. Capture the result first and test it:
Dim reply As String reply = InputBox("How many units?", "Unit count") If StrPtr(reply) = 0 Then MsgBox "Cancelled. Nothing changed." Exit Sub End IfStrPtr(reply) = 0is the reliable way to distinguish a true Cancel from an empty OK, which both look like""when tested with the plain equals operator. - Validate the content before you use it. A text box accepts anything, so check that the user typed what you need. To require a number, test with
IsNumeric(reply)and convert withCDbl(reply)orCLng(reply). To require a date, useIsDate(reply)andCDate(reply). To keep the answer inside a list, compare it against your allowed values. Show a clear message and exit or loop back to the prompt rather than letting bad data through. A simple loop that re-asks until valid is friendlier than a message box the user has to restart after. - Write the value where you need it. For example
Range("B2").Value = CDbl(reply)puts the number in a cell. UseActiveSheetdeliberately if the macro should always act on the visible sheet, or name the worksheet explicitly (Worksheets("Data")) so the macro does not depend on which sheet is in front. - Convert to the right data type at the boundary. The InputBox always returns a String. Assigning directly to a numeric variable forces conversion, but doing it explicitly with
CLng,CDbl,CDateorCBoolmakes the intent obvious and lets you trap conversion errors. Note that the conversion respects regional settings: a decimal comma in one region is a decimal point in another, which is why entering 10,5 in a US-configured Excel is read as text or rejected. - Consider
Application.InputBoxwhen you need a type check built in. It is a different function with the same name, called through the Application object, and it adds aTypeargument: 1 means number, 2 means text, 4 means logical, 8 means a cell reference (Range). With Type:=8, the user can click a cell and the function returns a Range object, which is handy for asking "which cell should I update?". The catch is that it is Excel-specific, whereas the plainInputBoxworks across the Office applications. - Save the file as .xlsm and test it from a clean state: close the workbook, reopen it, enable macros in the security banner if prompted, and run it from the Macros dialog. Testing inside the editor can hide problems that appear when the macro is launched normally.
Common Mistakes
- Testing only for an empty string to detect Cancel. An OK with nothing typed also returns
"", so useStrPtrto tell the two apart. - Forgetting that the result is text. Concatenating two inputs gives "23" rather than 5, and comparing numbers as text sorts "10" before "9".
- Putting the code in a sheet module or ThisWorkbook and then wondering why it does not appear in the Macros list. Standard modules are the right home for general-purpose prompts.
- Omitting the Title argument, which leaves the window with the application's default caption and looks unfinished.
- Trusting the input without validation, so a stray space or a full stop ends up in a numeric column and breaks later formulas.
- Saving as .xlsx. The code vanishes without warning when the file is saved in the non-macro format.
Tips
- Keep the prompt short and specific: "Enter the quantity to add (whole number)" tells the user what format is expected far better than "Input:".
- Use the Default argument to show the current value when editing existing data, so the user can confirm it with Enter instead of retyping.
- For yes/no decisions, a MsgBox with vbYesNo returns 6 for Yes and 7 for No and reads more naturally than asking the user to type "yes".
- Never use an InputBox to collect passwords. The typed text is visible in the box, and the string can end up in cells or in your code history.
- Wrap conversion calls in error handling when the input type matters, so a mistyped entry produces a polite message rather than a runtime error dialog.
Troubleshooting
If the macro will not run at all, check that macros are enabled and that the file is .xlsm. If it is missing from the Alt+F8 list, confirm the Sub is Public (the default) and sits in a standard module, and that it takes no arguments. If pressing F5 does nothing, the cursor may be outside a procedure; click inside the Sub first.
If you get a "Compile error: Expected: expression", look for a missing quote or ampersand in the prompt string. If the box appears but the variable is empty afterwards, check that you assigned the return value with = instead of calling InputBox as a statement. If a number arrives with the wrong value, suspect regional decimal and thousands separators, and either format the prompt example to match the user's region or validate with IsNumeric first.
If writing to a sheet fails, the target may be protected or the workbook may be shared; unprotect it or unshare it, or write to an unprotected location. If the macro worked yesterday and fails today, the workbook may have been saved in .xlsx, or the module may have been deleted.
If the InputBox Did Not Work
If the prompt never appears, the most common cause is that the macro did not actually execute. Step through it with F8 and watch each line highlight. If cancel detection misbehaves, swap the plain InputBox call for Application.InputBox with a Type argument so the function itself rejects wrong input. If your prompt needs more than one field, a built-in UserForm is the more maintainable answer: insert one from the editor and add text boxes, though that is a larger project than a single-line prompt.
Frequently Asked Questions
What does an InputBox return if the user clicks Cancel?
It returns an empty string in the variable, but StrPtr on that value is 0, which is how you detect a genuine Cancel versus an empty OK. Application.InputBox with a Type argument returns False on Cancel instead.
Can I use InputBox in Word, Access or Outlook?
Yes. The VBA InputBox function is available in every Office application that hosts VBA, with the same arguments and behaviour. Application.InputBox with cell references is Excel-only.
How do I make the user enter a number only?
Test with IsNumeric(reply) and convert with CDbl or CLng inside a loop that re-prompts until valid, or use Application.InputBox with Type:=1, which refuses text entries itself.
Can I pre-fill the input box?
Yes, pass the value as the third argument, for example InputBox("Quantity", "Edit", Range("B2").Value). The user can edit or accept it.
Why does my typed decimal get rejected?
The text is converted using the machine's regional settings. If Excel expects a decimal point strongly and the user types a comma, IsNumeric can fail. Match the example in the prompt to the local format.
Is InputBox safe for passwords?
No. Typed characters are visible, and the value can be written to cells, logs or memory. Use a dedicated secure method rather than a visible prompt.