VBA · User interaction
InputBox
Asks the user to type a value.
When to use it
InputBox asks the user to type a value and returns it as text. Application.InputBox is the typed version that can return ranges and numbers.
The code
- Code
name = InputBox("Enter the region")
Worked examples
Ask for text
name = InputBox("Enter the region") → The typed text, or "" if cancelled
Simple prompt.
With a default
n = InputBox("How many rows?", "Setup", 10) → With a title and default
Defaults help.
Detect Cancel
If StrPtr(answer) = 0 Then Exit Sub → Distinguishes Cancel from an empty entry
The Cancel test.
Worth knowing
- Validate with IsNumeric before using the answer as a number.
- Application.InputBox with Type:=1 forces a number and Type:=8 a range.
- Loop until the answer is valid.
Where it goes wrong
- Cancel returns an empty string, indistinguishable from typing nothing without the StrPtr trick.
- The result is always text from plain InputBox.
Related
Learn the moves here — or let Wauvel run them on your numbers.
$99/mo, everything included. Free for 14 days, no card.
One CFO-grade Excel tip a week
A short, practical email for finance operators — functions, shortcuts, and the moves that save an afternoon. Free, unsubscribe anytime.