In some cases, you can save yourself the trouble of creating a custom dialog box by using one of severalprebuilt dialog boxes. The sections that follow describe various dialog boxes that you can displaywithout creating a UserForm.
Using an Input Box
An
input box
is a simple dialog box that allows the user to make a single entry. For example, you canuse an input box to let the user enter text, a number, or even select a range. There are actually two waysto generate an
InputBox
: one by using a VBA function, and the other by using a method of the
Application
object.
The VBA InputBox function
The syntax for VBA's
InputBox
function is:
InputBox(prompt[,title][,default][,xpos][,ypos][,helpfile, context])
prompt
: Required. The text displayed in the InputBox.
title
: Optional. The caption of the InputBox window.
default
: Optional. The default value to be displayed in the dialog box.
xpos
,
ypos
: Optional. The screen coordinates of the upper-left corner of the window.
helpfile
,
context
: Optional. The help file and help topic.The
InputBox
function prompts the user for a single piece of information. The function always returns astring, so it may be necessary to convert the results to a value.The prompt may consist of about 1,024 characters (more or less, depending on the width of thecharacters used). In addition, you can provide a title for the dialog box and a default value and specifyits position on the screen. And you can specify a custom help topic; if you do, the input box includes aHelp button.The following example, whose output is shown inFigure 12-1, uses the VBA
InputBox
function to ask the user for his or her full name. The code then extracts the first name and displays a greeting in amessage box.