InputBox Method

Applies To

Application Object.

Description

Displays a dialog box for user input. Returns the information entered in the dialog box.

Syntax

object.InputBox(prompt, title, default, left, top, helpFile, helpContextID, type)

object

Required. The Application object.

prompt

Required. The message to be displayed in the dialog box. This may be a string, a number, a date, or a Boolean value.

title

Optional. The title for the input box. If this argument is omitted, the default title is "Input".

default

Optional. Specifies a value to be put in the edit box when the dialog box is initially displayed. If this argument is omitted, the edit box is left empty. This value may be a Range object.

left

Optional. Specifies an x position for the dialog box, in points, from the top left of the screen. One point is 1/72 inch.

top

Optional. Specifies a y position for the dialog box, in points, from the top left of the screen.

helpFile

Optional. The name of the online Help file for this input box. If the helpFile and helpContextID arguments are present, a Help button will appear in the dialog.

helpContextID

Optional. The context ID of the Help topic in helpFile.

type

Optional. Specifies the return data type. If this argument is omitted, the dialog box returns text. May have one of the following values:

Value

Meaning

0

A formula

1

A number

2

Text (a string)

4

A logical value (True or False)

8

A cell reference, as a Range object

16

An error value, such as #N/A

64

An array of values


You can use the sum of the allowable values for type. For example, for an input box that can accept text or numbers, set type equal to 1 + 2.

Remarks

Use InputBox to display a simple dialog box so you can enter information to be used in a macro. The dialog box has an OK and a Cancel button. If you choose the OK button, InputBox returns the value entered in the dialog box. If you choose the Cancel button, InputBox returns False.

If type = 0, InputBox returns the formula in the form of text, for example, "=2*PI()/360". If there are any references in the formula, they are returned as A1-style references. (Use ConvertFormula to convert between reference styles.)

If type = 8, InputBox returns a Range object. You must use the Set statement to assign the result to a Range object, as shown in the following example.


Set myRange = Application.InputBox(prompt := "Sample", type := 8)

If you do not use the Set statement, the variable is set to the value in the Range, rather than the Range itself.

If you use the InputBox method to ask the user for a formula, you must use the FormulaLocal property to assign the formula to a Range object. The input formula will be in the user's language. The InputBox method differs from the InputBox function because it allows selective validation of the user's input, and it can be used with Microsoft Excel objects, error values and formulas. Note that Application.InputBox calls the InputBox method; InputBox with no object qualifier calls the InputBox function.

Example

This example prompts the user for a number.


myNum = Application.InputBox("Enter a number")

This example prompts the user to select a cell on Sheet1. The example uses the type argument to ensure that the return value is a valid cell reference (a Range object).


Worksheets("Sheet1").Activate
Set myCell = Application.InputBox( _
    prompt:="Select a cell", Type:=8)