Read without ads and support Scribd by becoming a Scribd Premium Reader.
 
 
Part IV: Working with UserForms
Chapter List
Chapter 12: Custom Dialog Box AlternativesChapter 13: Introducing UserFormsChapter 14: UserForm ExamplesChapter 15: Advanced UserForm Techniques
Chapter 12: Custom Dialog Box Alternatives
Download CD Content 
Overview
In This Chapter
 Dialog boxes are, perhaps, the most important user interface element in Windows programs. Virtuallyevery Windows program uses them, and most users have a good understanding of how they work. Exceldevelopers implement custom dialog boxes by creating UserForms. However, VBA provides the meansto display some built-in dialog boxes. This chapter covers the following topics:
Using an input box to get user input
Using a message box to display messages or get a simple response
Selecting a file from a dialog box
Selecting a directory
Displaying Excel's built-in dialog boxesBefore I get into the nitty-gritty of creating UserForms, you might find it helpful to understand some of Excel's built-in tools that display dialog boxes. That's the focus of this chapter.
Before You Create That UserForm …
 
Page 1 of 117Part IV: Working with UserForms10/28/2010file://C:\Documents and Settings\FUNNY GUY\Local Settings\Temp\~hhA830.htm
 
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.
 
Page 2 of 117Part IV: Working with UserForms10/28/2010file://C:\Documents and Settings\FUNNY GUY\Local Settings\Temp\~hhA830.htm
 
 Figure 12-1: VBA's InputBox function at work.
Sub GetName()Dim UserName As StringDim FirstSpace As IntegerDo Until UserName <> ""UserName = InputBox("Enter your full name: ", _"Identify Yourself")LoopFirstSpace = InStr(UserName, " ")If FirstSpace <> 0 ThenUserName = Left(UserName, FirstSpace - 1)End IfMsgBox "Hello " & UserNameEnd Sub
Notice that this
InputBox
function is written in a
Do Until
loop to ensure that something is enteredwhen the input box appears. If the user clicks Cancel or doesn't enter any text,
UserName
contains anempty string, and the input box reappears. The procedure then attempts to extract the first name bysearching for the first space character (by using the
InStr
function) and then using the
Left
function toextract all characters before the first space. If a space character is not found, the entire name is used asentered.As I mentioned, the
InputBox
function always returns a string. If the string returned by the
InputBox
 function looks like a number, you can convert it to a value by using VBA's
Val
function. Or you can useExcel's InputBox method, which I describe in thenext section.Figure 12-2shows another example of the VBA
InputBox
function. The user is asked to fill in themissing word. This example also illustrates the use of named arguments. The prompt text is retrievedfrom a worksheet cell.
 
Page 3 of 117Part IV: Working with UserForms10/28/2010file://C:\Documents and Settings\FUNNY GUY\Local Settings\Temp\~hhA830.htm
Search History:
Searching...
Result 00 of 00
00 results for result for
  • p.
  • Notes
    Load more