InputBox 函数InputBox function

在对话框中显示提示,等待用户输入文本或单击按钮,然后返回包含文本框内容的字符串Displays a prompt in a dialog box, waits for the user to input text or click a button, and returns a String containing the contents of the text box.

语法Syntax

InputBox(prompt、[ title ]、[ default ]、[ xpos ]、[ ypos ]、[快速_帮助_语、上下文])InputBox(prompt, [ title ], [ default ], [ xpos ], [ ypos ], [ helpfile, context ])


InputBox 函数包含以下命名参数The InputBox function syntax has these named arguments:

PartPart 说明Description
promptprompt 必需项。Required. 字符串表达式在对话框中显示为消息。String expression displayed as the message in the dialog box. prompt 的最大长度约为 1024 个字符,具体取决于所使用的字符的宽度。The maximum length of prompt is approximately 1024 characters, depending on the width of the characters used. 如果_prompt_包含多行, 则可以使用回车符 (chr(13))、换行符 (chr(10)) 或回车换行符组合 ((chr (13) & ((chr (13)) 来分隔行。 ((chr(13) (**** 每个行的 Chr (10))。If prompt consists of more than one line, you can separate the lines by using a carriage return character (Chr(13)), a linefeed character (Chr(10)), or carriage return-linefeed character combination ((Chr(13) & (Chr(10)) between each line.
titletitle 可选。Optional. 对话框标题栏中显示的字符串表达式。String expression displayed in the title bar of the dialog box. 如果省略 title,则标题栏中将显示应用程序名称。If you omit title, the application name is placed in the title bar.
defaultdefault 可选。Optional. 文本框中显示的字符串表达式,在未提供其他输入时作为默认响应。String expression displayed in the text box as the default response if no other input is provided. 如果省略了 default,文本框将显示为空。If you omit default, the text box is displayed empty.
xposxpos 可选。Optional. 指定对话框的左边缘与屏幕的左边缘的水平距离(以缇为单位)的数值表达式Numeric expression that specifies, in twips, the horizontal distance of the left edge of the dialog box from the left edge of the screen. 如果省略了 xpos,对话框将水平居中。If xpos is omitted, the dialog box is horizontally centered.
yposypos 可选。Optional. 指定对话框的上边缘与屏幕的顶部的垂直距离(以缇为单位)的数值表达式。Numeric expression that specifies, in twips, the vertical distance of the upper edge of the dialog box from the top of the screen. 如果省略了 ypos,对话框将位于屏幕垂直方向往下大约三分之一的位置。If ypos is omitted, the dialog box is vertically positioned approximately one-third of the way down the screen.
helpfilehelpfile 可选。Optional. 用于标识帮助文件的字符串表达式,前者用于为对话框提供上下文相关的帮助。String expression that identifies the Help file to use to provide context-sensitive Help for the dialog box. 如果提供 helpfile,则也必须提供 contextIf helpfile is provided, context must also be provided.
contextcontext 可选。Optional. 帮助上下文数值的数值表达式,该数值由帮助作者为相应的帮助主题分配。Numeric expression that is the Help context number assigned to the appropriate Help topic by the Help author. 如果提供 context,则也必须提供 helpfileIf context is provided, helpfile must also be provided.

注释Remarks

同时提供有 helpfilecontext 时,用户可以按 F1 (Windows) 或 HELP (Macintosh) 以查看与 context 对应的帮助主题。When both helpfile and context are provided, the user can press F1 (Windows) or HELP (Macintosh) to view the Help topic corresponding to the context. 某些主机应用程序(例如,Microsoft Excel)还会在对话框中自动添加“帮助”按钮。Some host applications, for example, Microsoft Excel, also automatically add a Help button to the dialog box. 如果用户选择 "确定" 或按 Enter, InputBox函数将返回文本框中的任何内容。If the user chooses OK or presses ENTER, the InputBox function returns whatever is in the text box. 如果用户选择 "取消", 则该函数将返回零长度字符串 ("")。If the user chooses Cancel, the function returns a zero-length string ("").

备注

若除了第一个命名参数之外还要指定其他参数,则必须在表达式中使用 InputBoxTo specify more than the first named argument, you must use InputBox in an expression. 要省略某些位置参数,必须包含相应的逗号分隔符。To omit some positional arguments, you must include the corresponding comma delimiter.

示例Example

本示例显示了使用 InputBox 函数提示用户输入值的各种方式。This example shows various ways to use the InputBox function to prompt the user to enter a value. 如果省略了 x 和 y 位置,对话框将自动在各个轴居中。If the x and y positions are omitted, the dialog box is automatically centered for the respective axes. 如果用户MyValue选择 "确定" 或按 ENTER 键, 则该变量包含用户输入的值。The variable MyValue contains the value entered by the user if the user chooses OK or presses the ENTER key. 如果用户选择 "取消", 则返回零长度字符串。If the user chooses Cancel, a zero-length string is returned.

Dim Message, Title, Default, MyValue
Message = "Enter a value between 1 and 3"    ' Set prompt.
Title = "InputBox Demo"    ' Set title.
Default = "1"    ' Set default.
' Display message, title, and default value.
MyValue = InputBox(Message, Title, Default)

' Use Helpfile and context. The Help button is added automatically.
MyValue = InputBox(Message, Title, , , , "DEMO.HLP", 10)

' Display dialog box at position 100, 100.
MyValue = InputBox(Message, Title, Default, 100, 100)

另请参阅See also

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.