Excel 2010 Bible

(National Geographic (Little) Kids) #1

Chapter 41: Creating UserForms


839


The following is an example of how you can use the InputBox function:

CName = InputBox(“Customer name?”,”Customer Data”)

When this VBA statement is executed, Excel displays the dialog box shown in Figure 41.2. Notice
that this example uses only the first two arguments for the InputBox function and does not sup-
ply a default value. When the user enters a value and clicks OK, the value is assigned to the vari-
able CName. Your VBA code can then use that variable.

FIGURE 41.2

This dialog box is displayed by the VBA InputBox function.


The MsgBox function

The VBA MsgBox function is a handy way to display information and to solicit simple input from
users. I use the VBA MsgBox function in many of this book’s examples to display a variable’s
value. A simplified version of the MsgBox syntax is as follows:

MsgBox(prompt[,buttons][,title])

The elements are defined as follows:

l prompt: (Required) Text that is displayed in the message box

l (^) buttons: (Optional) The code for the buttons that are to appear in the message box
l title: (Optional) Text that appears in the message box’s title bar
You can use the MsgBox function by itself or assign its result to a variable. If you use it by itself,
don’t include parentheses around the arguments. The following example displays a message and
does not return a result:
Sub MsgBoxDemo()
MsgBox “Click OK to continue”
End Sub
Figure 41.3 shows how this message box appears.

Free download pdf