You cannot combine a message box and an input field in one built-in Excel dialog, so the standard way to ask the user for a value is to use the InputBox function in a VBA macro. InputBox shows a small prompt with a text field and OK/Cancel buttons, and returns whatever the user types as text. If you need a full message box plus input, you either use InputBox on its own or build a custom dialog on a UserForm. Everything below is done in the Excel Visual Basic Editor, and no programming experience is required to follow it.

What You'll Need

  • Excel for Windows or Excel for Mac (the menu names below are the Windows ones; Mac labels are similar but sit under different menus).
  • A workbook saved as .xlsm (macro-enabled) if you want to keep the code. If you save as .xlsx, Excel removes the macros on close.
  • The Developer tab visible, or you can open the editor with the Alt+F11 shortcut.
  • Access to the VBA project. Any normal workbook works; protected or shared workbooks may block editing.

To show the Developer tab, right-click the ribbon, choose Customize the Ribbon, tick Developer in the right-hand list, and click OK. Menu labels can differ slightly by Excel version and region.

Step-by-Step

  1. Open the Visual Basic Editor. Press Alt+F11, or go to Developer > Visual Basic. The editor opens in its own window.
  2. Insert a module. In the editor, click Insert > Module. A blank code window appears in the right-hand pane. Macros in a standard module can be seen and run from the normal Excel interface, which is what you want for a general-purpose input tool.
  3. Write a basic input box. Type the skeleton below, using your own prompt text:
    Sub AskForName()
        Dim userInput As String
        userInput = InputBox("Type your name:", "Name Please")
        MsgBox "Hello, " & userInput
    End Sub
    The first item in the brackets is the prompt shown inside the box, and the second is the title on the window bar.
  4. Run it. Put the cursor inside the procedure and press F5, or close the editor and press Alt+F8, select AskForName, and click Run. The input box appears, you type a value, and a message box then shows the result.
  5. Add a default value. The third item in the brackets is the text that appears in the field already. InputBox("How many rows?", "Rows", "10") saves the user typing a common answer.
  6. Handle Cancel properly. When the user clicks Cancel or closes the box with the X, InputBox returns an empty string. If OK is clicked with nothing typed, it also returns an empty string. Check before acting:
    If userInput = "" Then
        MsgBox "Nothing entered - no changes made."
        Exit Sub
    End If
    You cannot tell Cancel apart from a blank OK with plain InputBox. If that distinction matters, use a UserForm instead.
  7. Validate the value. Everything typed comes back as text. To require a number, test it and re-ask or stop:
    If Not IsNumeric(userInput) Then
        MsgBox "Please enter a number."
        Exit Sub
    End If
    Then convert with CLng(userInput) or CDbl(userInput) before using it in formulas or ranges.
  8. Use the answer. A typical use is writing a value into a cell: Range("A1").Value = userInput. You can equally feed it into a filter, a lookup, a filename for saving, or a loop count.
  9. Restrict free typing with the Type argument (optional). InputBox accepts a Type parameter that changes the field into a number, formula or range selector. For example, Application.InputBox("Pick a cell:", "Range", Type:=8) lets the user click a cell and returns the range object. This is Application.InputBox, not the plain InputBox, and the argument order is different, so do not mix the two.
  10. Save as macro-enabled. Use File > Save As, choose Excel Macro-Enabled Workbook (*.xlsm), and click Save. Then test once more after reopening, because a security prompt may block macros on the next open.

Common Mistakes

  • Saving as .xlsx. The code runs until you close the file, then disappears. Always use .xlsm.
  • Assuming a number comes back as a number. InputBox returns a string; without conversion, arithmetic quietly gives wrong results or a type mismatch.
  • Ignoring Cancel. A user who cancels returns an empty string, and code that writes it straight to a cell can wipe existing data.
  • Mixing up InputBox and Application.InputBox. They take arguments in a different order and handle Cancel differently; pick one and stay consistent.
  • Putting code in the wrong place. Code pasted into a sheet module instead of a standard module may not appear in the macro list, and sheet-level procedures behave differently.
  • Expecting a message box and input field in one dialog. There is no built-in combined dialog; use InputBox or a UserForm.

Tips

  • Keep prompts short and say exactly what format you want, for example "Enter a date as DD/MM/YYYY".
  • Store frequently used prompts as constants at the top of the module so you only edit one place.
  • Use vbOKCancel style thinking: if you need Yes/No or OK/Cancel choices backed by a real dialog, use MsgBox for the question and InputBox only when text entry is genuinely needed.
  • For repeated or complex input, build a UserForm with labelled text boxes and a Validate button. It lets you distinguish Cancel, enforce formats, and pre-fill fields.
  • Test with awkward input: empty, spaces, letters in a numeric field, very long text, and Cancel.
  • Keep a copy of the workbook before editing macros, and note that macro security settings may require enabling content each time.

Troubleshooting

The macro is not in the list. Make sure the code is in a standard module (Insert > Module), the procedure starts with Sub and is not marked Private, and the workbook is macro-enabled. Close and reopen the Alt+F8 dialog.

Nothing happens or a security bar appears. Macros are likely disabled. Check the yellow bar below the ribbon and choose to enable content, or adjust Trust Center macro settings if your organisation allows it. Do not lower security settings on a work machine without checking with IT.

Compile error or "Expected: end of statement". Usually a missing quote or bracket in the prompt text, or curly quotation marks copied from a website. Retype straight quotes in the editor.

The result is always empty. The user may be clicking Cancel, or the variable is not assigned. Confirm you use userInput = InputBox(...) rather than calling it without assigning the result.

Type mismatch at runtime. Text went into a numeric operation. Add IsNumeric checks and convert with CLng or CDbl.

The input box appears behind Excel. Click the title bar of the workbook to bring it forward, or press Alt+Tab. Some add-ins can interrupt focus; restarting Excel usually clears it.

The macro was there yesterday and now it is gone. The file was probably saved as .xlsx. Restore a backup if you have one, then save as .xlsm going forward.

Frequently Asked Questions

Can I put a message box and an input field in the same dialog?

Not with built-in Excel dialogs. InputBox gives you a prompt line and a text field, which is the closest single-dialog option. For more layout, use a UserForm.

How do I tell whether the user clicked Cancel?

Plain InputBox returns an empty string for both Cancel and an empty OK. Application.InputBox returns False on Cancel, so it can be distinguished. A UserForm also reports Cancel through its own button.

Why does my input come back as text when I type a number?

That is how InputBox works by design. Convert it with CInt, CLng or CDbl after checking IsNumeric.

Can the input box use a password mask?

The standard InputBox cannot hide typed characters. A UserForm text box with its PasswordChar property set is the usual way to mask entry. Masking only hides the characters on screen; treat stored passwords carefully.

Does this work in Excel for Mac?

Yes, VBA and InputBox are available in Excel for Mac. Shortcuts and menu names differ, and some Windows-only features such as certain UserForm controls may not behave identically.

Can an input box collect a cell range instead of text?

Yes, with Application.InputBox and Type:=8, the user can select cells and you receive a range object. Remember the arguments are ordered differently from the plain function.