VbaFunctionsBeginner

InputBox

Prompts the user for a single text value.

Review the syntaxStudy the examplesOpen the coding app
value = InputBox(prompt, title, default)

This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.

What it does

Overview

Prompts the user for a single text value.

InputBox is a quick way to collect one value during macro execution, such as a sheet name, reporting month, or lookup code. It is acceptable for lightweight prompts, but fragile for validated numeric workflows where a user form would be safer.

Quick reference

Syntax

value = InputBox(prompt, title, default)

Inputs

Parameters

promptString · Question shown in the dialog.
title (optional)String · Optional dialog title.
default (optional)String · Optional prefilled value.

See it in practice

Examples

1

Prompt for a target worksheet

Dim targetSheet As String

targetSheet = InputBox("Enter the worksheet name to export", "Export target", "Sheet1")
If Len(targetSheet) > 0 Then
    Worksheets(targetSheet).Activate
End If

Always validate the returned text before using it as a workbook reference.

Debug faster

Common Errors

1

Run-time error

Cause: Assuming the entered text is valid and using it directly as a sheet, range, or number.

Fix: Check for an empty string, validate the value, and guard the downstream object lookup.

Runtime support

Compatibility

Excel desktop VBA

Source: Microsoft Learn Office VBA reference

Common questions

Frequently Asked Questions

Prompts the user for a single text value.

prompt: Question shown in the dialog. title: Optional dialog title. default: Optional prefilled value.

Run-time error: Check for an empty string, validate the value, and guard the downstream object lookup.